我看到有一些方法来获得应用程序文件夹路径:

应用程序。StartupPath System.IO.Path.GetDirectoryName ( System.Reflection.Assembly.GetExecutingAssembly () .Location) AppDomain.CurrentDomain.BaseDirectory System.IO.Directory.GetCurrentDirectory () 环境。CurrentDirectory System.IO.Path.GetDirectoryName ( System.Reflection.Assembly.GetExecutingAssembly () . getname () .CodeBase) System.IO.Path.GetDirectory (Application.ExecutablePath)

根据具体情况,最好的方法是什么?


当前回答

Application.StartupPathand 7. System.IO.Path.GetDirectoryName(Application.ExecutablePath) - Is only going to work for Windows Forms application System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location) Is going to give you something like: "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal-services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83_4b63d101" which is where the page that you are running is. AppDomain.CurrentDomain.BaseDirectory for web application could be useful and will return something like "C:\\hg\\Services\\Services\\Services.Website\\" which is base directory and is quite useful. System.IO.Directory.GetCurrentDirectory() and 5. Environment.CurrentDirectory

将为您提供进程从哪里启动的位置-因此,对于运行在Visual Studio调试模式下的web应用程序,例如“C:\\Program Files (x86)\\IIS Express”

System.IO.Path.GetDirectoryName ( System.Reflection.Assembly.GetExecutingAssembly () . getname () .CodeBase)

将为您提供运行代码的.dll的位置,对于web应用程序,可以是“file:\\C:\\hg\\Services\\Services\\Services”。网站\ \ bin "

现在,例如控制台应用程序点2-6将是。exe文件所在的目录。

希望这能为您节省一些时间。

其他回答

请注意,并非所有这些方法都会返回相同的值。在某些情况下,它们可以返回相同的值,但要注意,它们的目的是不同的:

Application.StartupPath

返回StartupPath参数(可以在运行应用程序时设置)

System.IO.Directory.GetCurrentDirectory()

返回当前目录,该目录可能是应用程序所在的文件夹,也可能不是。Environment.CurrentDirectory也是如此。如果您在DLL文件中使用它,它将返回进程运行的路径(在ASP.NET中尤其如此)。

这个System.IO.Path.GetDirectory(Application.ExecutablePath)变成了System.IO.Path.GetDirectoryName(Application.ExecutablePath)

Application.StartupPathand 7. System.IO.Path.GetDirectoryName(Application.ExecutablePath) - Is only going to work for Windows Forms application System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location) Is going to give you something like: "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal-services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83_4b63d101" which is where the page that you are running is. AppDomain.CurrentDomain.BaseDirectory for web application could be useful and will return something like "C:\\hg\\Services\\Services\\Services.Website\\" which is base directory and is quite useful. System.IO.Directory.GetCurrentDirectory() and 5. Environment.CurrentDirectory

将为您提供进程从哪里启动的位置-因此,对于运行在Visual Studio调试模式下的web应用程序,例如“C:\\Program Files (x86)\\IIS Express”

System.IO.Path.GetDirectoryName ( System.Reflection.Assembly.GetExecutingAssembly () . getname () .CodeBase)

将为您提供运行代码的.dll的位置,对于web应用程序,可以是“file:\\C:\\hg\\Services\\Services\\Services”。网站\ \ bin "

现在,例如控制台应用程序点2-6将是。exe文件所在的目录。

希望这能为您节省一些时间。

如果你知道根目录:

string rootPath = Path.GetPathRoot(Application.StartupPath)

我成功地用过这个

System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

它甚至可以在linqpad内部工作。