我想创建一个转换文件的程序。我希望用户能够将可执行文件放在任何目录,当执行该程序(双击.exe),我希望程序处理当前文件夹内的所有文件,其中exe文件存在。程序如何确定当前正在执行的路径?

我尝试了System.Windows.Forms.Application.StartupPath,但这似乎是错误的方式。

什么好主意吗?


当前回答

如果你想要exe路径,你可以使用System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

其他回答

如果你想要exe路径,你可以使用System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

使用应用程序。StartupPath在我看来是最好的结果。

如果你想要项目文件夹的路径,你也可以这样做:

        DirectoryInfo di = new DirectoryInfo(".");
       //OR 
        DirectoryInfo di = new DirectoryInfo("../../../");

        Console.WriteLine(di.FullName);

结果: D: \开发\ c# \ ProjectFolder \ ProjectFolder \

试试这个:

System.Environment.CurrentDirectory

对于。net CORE使用System.AppContext.BaseDirectory

(替换AppDomain.CurrentDomain.BaseDirectory)