获取应用程序文件夹路径的最佳方法

我看到有一些方法可以获得应用程序的文件夹路径。

  1. "应用程序.启动路径"(Application.StartupPath)。
  2. System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)
  3. AppDomain.CurrentDomain.BaseDirectory
  4. System.IO.Directory.GetCurrentDirectory()。 5.5. "环境.当前目录"(Environment.CurrentDirectory)。 6.System.IO.Path.GetDirectoryName() System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)。 7.System.IO.Path.GetDirectory(Application.ExecutablePath)

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

对该问题的评论 (4)
解决办法

AppDomain.CurrentDomain.BaseDirectory对于访问位置相对于应用程序安装目录的文件可能是最有用的。

在ASP.NET应用程序中,这将是应用程序的根目录,而不是bin子文件夹 - 这可能是你通常想要的。在一个客户程序中,它将是包含主可执行文件的目录。

在VSTO 2005应用程序中,它将是包含你的应用程序的VSTO管理程序集的目录,而不是,比如,Excel可执行文件的路径。

其他的可能会根据你的环境返回不同的目录--例如见@Vimvq1987'的答案。

CodeBase是找到文件的地方,可以是一个以http:// 开始的URL。在这种情况下,Location可能是汇编下载缓存。CodeBase不保证在GAC中的程序集被设置。

评论(10)
  1. Application.StartupPath和7. System.IO.Path.GetDirectoryName(Application.ExecutablePath)-只对[Windows Forms应用程序][1]起作用。

  2. System.IO.Path.GetDirectoryName() System.Reflection.Assembly.GetExecutingAssembly().Location)

会给你这样的东西: "C:\\Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/legal-services/e84f415e/96c98009/assembly/dl3/42aaba80/bcf9fd83_4b63d101"这就是你正在运行的页面所在。

  1. Web应用程序的AppDomain.CurrentDomain.BaseDirectory可能会很有用,会返回类似"C:\hg\\Services\\\Services\\Services.Website\"这是基目录,相当有用。

  2. System.IO.Directory.GetCurrentDirectory()和5. Environment.CurrentDirectory

将会得到该进程被启动的位置--所以对于从Visual Studio在调试模式下运行的web应用程序,类似于""C:\Program Files (x86)\IIS Express" "这样的内容

  1. System.IO.Path.GetDirectoryName()。 System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

将会得到运行代码的.dll所在的位置,对于web应用来说,可能是"file:\C:\hg\Services\Services\Services.Website\bin"

现在以控制台应用为例,2-6点将是.exe文件所在的目录。

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

[1]: https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath(v=vs.110).aspx

评论(2)

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

Application.StartupPath

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

System.IO.Directory.GetCurrentDirectory()

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

评论(3)

对于一个网络应用程序,要获得当前的网络应用程序根目录,一般要通过网页调用当前传入的请求。

HttpContext.Current.Server.MapPath();

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

以上代码说明

评论(0)

根据我的经验,最好的方式是以上几种方式的结合。

  1. System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase。 会给你bin文件夹
  2. Directory.GetCurrentDirectory()。 在.Net Core上可以正常工作,但在.Net上不行,它会给你项目的根目录。

  3. "System.AppContext.BaseDirectory "和 "AppDomain.CurrentDomain.BaseDirectory"。 在.Net中可以正常工作,但在.Net core中不行,并且会给你项目的根目录。

在一个应该是针对.Net和.Net core的类库中,我检查是哪个框架在托管这个库,然后选择一个或另一个。

评论(0)

我从一个Windows服务通过Win32 API在实际登录的用户会话中启动了一个进程(在任务管理器会话1而不是0)。 在这里我们可以知道,哪个变量是最好的。

对于上述问题中的7种情况,结果如下。

Path1: C:\Program Files (x86)\MyProgram
Path2: C:\Program Files (x86)\MyProgram
Path3: C:\Program Files (x86)\MyProgram\
Path4: C:\Windows\system32
Path5: C:\Windows\system32
Path6: file:\C:\Program Files (x86)\MyProgram
Path7: C:\Program Files (x86)\MyProgram

也许对一些人有帮助,做同样的事情,当你搜索到最佳的变量时,你的案例。

评论(1)

我已经成功地使用了这个

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

即使在[linqpad.][1]内也能工作。

[1]: http://www.linqpad.net/

评论(2)

根目录:

DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
var driverPath = cDrive.RootDirectory;
DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
var driverPath = cDrive.RootDirectory;
评论(1)

这个"System.IO.Path.GetDirectory(Application.ExecutablePath)&quot。 改为 System.IO.Path.GetDirectoryName(Application.ExecutablePath)

评论(0)

如果你知道获取根目录。

string rootPath = Path.GetPathRoot(Application.StartupPath)
评论(0)