获得应用程序文件夹路径的最佳方法 [英] Best way to get application folder path
问题描述
我看到有一些方法来获取应用程序文件夹路径:
I see that there are some ways to get the application folder path:
-
Application.StartupPath
-
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly()位置)
-
AppDomain.CurrentDomain.BaseDirectory
-
System.IO.Directory.GetCurrentDirectory()
-
Environment.CurrentDirectory
-
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly()的GetName()。codeBase的)
-
System.IO.Path.GetDirectory(Application.ExecutablePath)
Application.StartupPath
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)
AppDomain.CurrentDomain.BaseDirectory
System.IO.Directory.GetCurrentDirectory()
Environment.CurrentDirectory
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
System.IO.Path.GetDirectory(Application.ExecutablePath)
什么是最好的方式根据不同的情况?
What is the best way depending on the situation?
推荐答案
AppDomain.CurrentDomain.BaseDirectory
可能是用于访问文件的位置是相对于应用程序的最有用安装目录。
AppDomain.CurrentDomain.BaseDirectory
is probably the most useful for accessing files whose location is relative to the application install directory.
在ASP.NET应用程序中,这将是应用程序的根目录,而不是bin子文件夹 - 这可能是你平时想。在客户端应用程序,这将是包含主可执行文件的目录。
In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.
在一个VSTO 2005应用程序,这将是包含VSTO的目录管理组件为您的应用程序,而不是,说,路径到Excel的可执行文件。
In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.
其他人可能返回不同的目录根据您的环境 - 比如看@ Vimvq1987的答案
The others may return different directories depending on your environment - for example see @Vimvq1987's answer.
codeBase的
是一个文件被发现,并可以以http开头的URL的地方://。在这种情况下,位置
将可能是组装下载缓存。 codeBase的,不能保证在 GAC 组件进行设置。
CodeBase
is the place where a file was found and can be a URL beginning with http://. In which case Location
will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.
这篇关于获得应用程序文件夹路径的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!