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

C# 在中获取项目中所有文件的基本路径?,c#,openfiledialog,C#,Openfiledialog,假设我有一个文件路径: 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 = Ass

假设我有一个文件路径:

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;
但两者都返回不正确的值。

您可以使用此代码

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
DirectoryInfo Di=Directory.GetParent(@“C:\Users\my\u name\Desktop\my\u project\bin\debug\my\u project.exe”);
//这将为您提供调试文件夹
int DirectoryLevel=3;
for(int i=1;i
“.\..\”
-您的exe不“知道”其源代码的文件夹结构。可能重复的问题我在发布自己的问题之前快速查看了该问题,发现它没有适合我的答案。是否可以在静态上下文中使用此问题?我不相信这是真的。我收到错误(CS0120):在静态类中使用非静态字段、方法或属性“AppDomain.BaseDirectory”时,需要对象引用。有没有可能解决这个问题?虽然这个代码片段可以解决这个问题,但它确实有助于提高您的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满您的代码,因为这会降低代码和解释的可读性!很酷很高兴帮助你