假设我的文件路径为:
C:\Users\my_name\Desktop\my_project\bin\debug\my_project.exe
如何获得以下文件路径以在中使用OpenFileDialog
?
C:\Users\my_name\Desktop\my_project\
编辑:
我已经尝试了以下方法。
var path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent?.ToString();
var path = Assembly.GetExecutingAssembly().CodeBase;
但是两者都返回不正确的值。
您可以使用以下逻辑:
DirectoryInfo Di = Directory.GetParent(@"C:\Users\my_name\Desktop\my_project\bin\debug\my_project.exe");
// Which will give you the debug folder
int DirectoryLevel = 3;
for (int i = 1; i < DirectoryLevel; i++)
{
Di = Di.Parent;
// Which will give you the bin fodler when i =1
// Which will give you the my_project folder when i =2
}
string currentDirectory = Di.FullName; // Give the path
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句