运行应用程序的可执行目录?

JPJ*_*edi 18 vb.net executable desktop-application visual-studio-2008

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

System.AppDomain.CurrentDomain.BaseDirectory()
Run Code Online (Sandbox Code Playgroud)

当我在我的本地机器上使用&"/images/image.jpg"运行上述语句时,它运行正常,但是当我在另一台机器上安装该应用程序时,它说它无法找到该文件,并且有一些额外的路径信息.

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

谢谢!

Der*_*mba 27

这是google上的第一篇文章,所以我想我会发布不同的可用方式以及它们的比较方式.不幸的是我无法弄清楚如何在这里创建一个表,所以它是一个图像.每个代码都使用完全限定名称在图像下方.

在此输入图像描述

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))
Run Code Online (Sandbox Code Playgroud)


Jus*_*ner 25

Dim strPath As String = System.IO.Path.GetDirectoryName( _
    System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Run Code Online (Sandbox Code Playgroud)

摘自如何:确定执行应用程序的路径(MSDN)

  • strPath包含**文件**:\\ c:\\ xxxxxx (7认同)

MrC*_*vin 13

Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath
Run Code Online (Sandbox Code Playgroud)


小智 11

在记住环境课之前,我需要知道这一点并来到这里.

如果其他人有这个问题,请使用:Environment.CurrentDirectory.

例:

Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)
Run Code Online (Sandbox Code Playgroud)

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

C:\Development\solution folder\application folder\bin\debug
Run Code Online (Sandbox Code Playgroud)

这是我需要的确切行为,简单而直接.

  • 最小、最简单的解决方案,带格式!+1,谢谢。 (3认同)
  • 是的,通过将设置文件保存在应用程序的根路径中,这有助于使应用程序真正可移植。感谢您提供简单的解决方案! (2认同)
  • @vr_driver 这很糟糕。为了可移植,应使用可执行文件的父目录,而不是当前目录,当前目录可以与前者不同。例如,如果在“%windir%”中运行的脚本使用绝对路径调用程序,它将查找“%windir%\data\”,这很糟糕。 (2认同)

Mik*_*scu 5

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