【问题标题】:Executable directory where application is running from?运行应用程序的可执行目录?
【发布时间】:2011-02-05 07:30:44
【问题描述】:

我需要获取运行我的应用程序的路径(不是可执行文件):

System.AppDomain.CurrentDomain.BaseDirectory()

当我在本地机器上使用 & "/images/image.jpg" 运行上述语句时,它工作正常,但是当我在另一台机器上安装应用程序时,它说它找不到文件并且有很多额外的路径资料一些。

我只需要运行应用程序的目录。我正在使用 Visual Studio 2008 在 VB.NET 中进行编码。

谢谢!

【问题讨论】:

  • 对 vs 2008 不太了解,但是当您执行 dirinfo 时,您不会从应用程序的位置获取 dirinfo。正在运行吗?
  • Get programs path?的可能重复

标签: vb.net visual-studio-2008 executable desktop-application


【解决方案1】:

这是谷歌上的第一篇文章,所以我想我会发布不同的可用方式以及它们的比较方式。不幸的是,我不知道如何在这里创建一个表格,所以它是一个图像。每个代码都使用完全限定名称在图像下方。

My.Application.Info.DirectoryPath

Environment.CurrentDirectory

System.Windows.Forms.Application.StartupPath

AppDomain.CurrentDomain.BaseDirectory

System.Reflection.Assembly.GetExecutingAssembly.Location

System.Reflection.Assembly.GetExecutingAssembly.CodeBase

New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase)

Path.GetDirectoryName(Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path)))

Uri.UnescapeDataString((New System.UriBuilder(System.Reflection.Assembly.GetExecutingAssembly.CodeBase).Path))

--- 2021 年 10 月 18 日编辑:

叹息...如果使用 net5.0 或 net6.0 并将应用程序作为单文件包发布,则上述方法均无效。我现在得到的最好的是:

// This will give you the directory but not the assembly
string basedir = AppContext.BaseDirectory;
// Before you package the app as a single file bundle, you will get the dll.  
// But after you publish it, you'll get the exe. 
string pathToExecutable = Environment.GetCommandLineArgs()[0].Replace(".dll", ".exe");

【讨论】:

  • System.AppDomain.CurrentDomain.BaseDirectory() 在控制台/服务应用程序中也很好用!
【解决方案2】:
Dim strPath As String = System.IO.Path.GetDirectoryName( _
    System.Reflection.Assembly.GetExecutingAssembly().CodeBase)

取自HOW TO: Determine the Executing Application's Path (MSDN)

【讨论】:

  • strPath 包含 文件:\\c:\\xxxxxx
  • 这是不安全的,例如当它被用来打开配置文件时会导致System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: URI formats are not supported
  • 此链接已过期 :(.
【解决方案3】:

在我记得环境类之前,我需要知道这一点并来到这里。

如果其他人遇到此问题,请使用:Environment.CurrentDirectory

例子:

Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)

在调试模式下从 Visual Studio 运行时:

C:\Development\solution folder\application folder\bin\debug

这正是我需要的行为,而且非常简单明了。

【讨论】:

  • 最小、最简单的解决方案,带格式! +1,谢谢。
  • 是的,通过将设置文件保存在应用程序的根路径中,这有助于使应用程序真正可移植。感谢您提供简单的解决方案!
  • @vr_driver 这很糟糕。对于可移植,应该使用可执行文件的父目录,而不是当前目录,这可能与前者不同。例如,如果在%windir% 中运行的脚本使用绝对路径调用程序,它将查找` %windir%\data\ `,这很糟糕。
  • @jason-alls:您的示例输出不是 100% 正确的。使用您的代码,您将获得 C:\Development\solution folder\application folder\bin\debug\DATA,因为您将其添加到您的值中,来自 Environment.CurrentDirectory
  • 这是对我有用的答案。所有应用程序属性都返回基于 .Net 框架的结果,这返回了我的应用程序正在运行的实际目录。
【解决方案4】:
Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath

【讨论】:

    【解决方案5】:

    您可以使用 Application 类的静态 StartupPath 属性。

    【讨论】:

      【解决方案6】:

      你可以这样写:

      Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")
      

      【讨论】:

      • 这种方法存在缺陷,因为程序集不一定与正在执行的程序集位于同一目录中。
      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多