我在以下位置有EXE:
C:\Projects\Bin\Sample.EXE
Run Code Online (Sandbox Code Playgroud)
我想在C#中获取这样的路径:
C:\Projects\
Run Code Online (Sandbox Code Playgroud)
一种方法是:
string str = "C:\\Projects\\Bin\\Sample.EXE"
string res = str.Replace("Bin", "")
Run Code Online (Sandbox Code Playgroud)
但这不是一种有效的方法.我的Bin文件夹可以改为Bin1,Bin2等等......所以Bin名字是不是恒定的.它也可以C:\\Projects\\Debug\\Sample.EXE.基本上,我想在目录结构中向上移动一级.
你能给我提供示例代码吗?
以下是我要查找的示例代码:
@marc_s代码
这与上面两个问题完全不同,我没有使用前面的两个链接找到我的问题的解决方案.
获取当前正在执行的程序集的位置,并从该目录上升一级 - 如下所示:
-- get path of the executing assembly
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
-- get the parent directory for that path
string parentPath = Path.GetFullPath(Path.Combine(currentPath, ".."));
Run Code Online (Sandbox Code Playgroud)