【发布时间】:2011-08-27 20:00:01
【问题描述】:
我看到有一些方法可以获取应用程序文件夹路径:
Application.StartupPathSystem.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)AppDomain.CurrentDomain.BaseDirectorySystem.IO.Directory.GetCurrentDirectory()Environment.CurrentDirectorySystem.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)System.IO.Path.GetDirectory(Application.ExecutablePath)
根据情况,最好的方法是什么?
【问题讨论】:
-
为什么我们有很多方法可以获取应用程序的路径。我认为每种方式都有其原因。
-
#6 中有错误:应为:System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), System.IO.Path.GetDirectoryName(Application.ExecutablePath)跨度>
-
#6 万岁,当我在一个 Web 项目中时,我不希望在我的 IoC 加载库中使用 Server.MapPath 逻辑,它本质上不是特定于 Web 的
-
我们现在有了可靠的
IHostEnvironment.ContentRootPath,通过注入的IHostEnvironment依赖项(其中包含其他有用的东西)访问。 -
还有
Process.GetCurrentProcess().MainModule.FileName。