获取项目中所有文件的基本路径?

Get the base path of all files in a project in?

假设我有一个文件路径:

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;

但是两个 return 值都不正确。

您可以使用此代码

string path = System.AppDomain.BaseDirectory + "//bin//debuge//filename.exe";

您可以使用以下逻辑:

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