C# 获取应用程序文件夹路径的最佳方法
我发现有一些方法可以获取应用程序文件夹路径:C# 获取应用程序文件夹路径的最佳方法,c#,.net,C#,.net,我发现有一些方法可以获取应用程序文件夹路径: Application.StartupPath System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutionGassembly().Location) AppDomain.CurrentDomain.BaseDirectory System.IO.Directory.GetCurrentDirectory() Environment.CurrentDirectory
Application.StartupPath
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutionGassembly().Location)
AppDomain.CurrentDomain.BaseDirectory
System.IO.Directory.GetCurrentDirectory()
Environment.CurrentDirectory
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase)
System.IO.Path.GetDirectory(Application.ExecutablePath)
根据具体情况,最好的方法是什么?请注意,并非所有这些方法都返回相同的值。在某些情况下,它们可以返回相同的值,但请注意,它们的用途不同:
Application.StartupPath
返回StartupPath
参数(可在运行应用程序时设置)
返回当前目录,该目录可能是应用程序所在的文件夹,也可能不是。
Environment.CurrentDirectory
也是如此。如果在DLL文件中使用此方法,它将返回进程运行的路径(在ASP.NET中尤其如此)。注意,并非所有这些方法都返回相同的值。在某些情况下,它们可以返回相同的值,但请注意,它们的用途不同:
Application.StartupPath
返回StartupPath
参数(可在运行应用程序时设置)
返回当前目录,该目录可能是应用程序所在的文件夹,也可能不是。
Environment.CurrentDirectory
也是如此。如果您在DLL文件中使用它,它将返回进程运行的路径(在ASP.NET中尤其如此)。AppDomain.CurrentDomain.BaseDirectory
对于访问位置相对于应用程序安装目录的文件可能最有用
在ASP.NET应用程序中,这将是应用程序根目录,而不是bin子文件夹-这可能是您通常想要的。在客户端应用程序中,它将是包含主可执行文件的目录
在VSTO 2005应用程序中,它将是包含应用程序的VSTO托管程序集的目录,而不是Excel可执行文件的路径
其他人可能会根据您的环境返回不同的目录-例如,请参阅@Vimvq1987的答案
CodeBase
是找到文件的地方,可以是以http:///开头的URL。在这种情况下,位置
可能是程序集下载缓存。不保证为中的程序集设置代码库
更新
现在(.NET Core、.NET Standard 1.3+或.NET Framework 4.6+)最好使用
AppContext.BaseDirectory
,而不是AppDomain.CurrentDomain.BaseDirectory
。两者都是等效的,但是。AppDomain.CurrentDomain.BaseDirectory
对于访问位置相对于应用程序安装目录的文件可能最有用
在ASP.NET应用程序中,这将是应用程序根目录,而不是bin子文件夹-这可能是您通常想要的。在客户端应用程序中,它将是包含主可执行文件的目录
在VSTO 2005应用程序中,它将是包含应用程序的VSTO托管程序集的目录,而不是Excel可执行文件的路径
其他人可能会根据您的环境返回不同的目录-例如,请参阅@Vimvq1987的答案
CodeBase
是找到文件的地方,可以是以http:///开头的URL。在这种情况下,位置
可能是程序集下载缓存。不保证为中的程序集设置代码库
更新
现在(.NET Core、.NET Standard 1.3+或.NET Framework 4.6+)最好使用
AppContext.BaseDirectory
,而不是AppDomain.CurrentDomain.BaseDirectory
。两者都是等效的,但是。对于web应用程序,要获取当前web应用程序根目录,通常通过网页调用当前传入的请求:
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
对于web应用程序,要获取当前web应用程序根目录,通常通过网页调用当前传入的请求:
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
Application.StartupPath
和7System.IO.Path.GetDirectoryName(Application.ExecutablePath)
-仅适用于System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutionGassembly().Location)
将为您提供如下内容:“C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83\u 4b63d101”
,这就是您正在运行的页面所在的位置AppDomain.CurrentDomain.BaseDirectory
对于web应用程序可能很有用,并将返回类似于“C:\\hg\\Services\\Services\\Services.Website\\”
的内容,这是基本目录,非常有用System.IO.Directory.GetCurrentDirectory()
和5<代码>环境.当前目录“C:\\Program Files(x86)\\IIS Express”
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase)
.dll
的位置,对于可能是的web应用程序,“文件:\\C:\\hg\\Services\\Services\\Services.Website\\bin”
例如,现在控制台应用程序点2-6将是.exe
文件所在的目录
希望如此
System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
string rootPath = Path.GetPathRoot(Application.StartupPath)
DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
var driverPath = cDrive.RootDirectory;