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

时间:2010-04-07 14:54:23

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

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

System.AppDomain.CurrentDomain.BaseDirectory()

当我用&运行上述声明时“/images/image.jpg”在我的本地机器上工作正常但是当我在另一台机器上安装应用程序时,它说它找不到文件,并且有一些额外的路径信息。

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

谢谢!

6 个答案:

答案 0 :(得分:26)

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

enter image description here

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))

答案 1 :(得分:25)

Dim strPath As String = System.IO.Path.GetDirectoryName( _
    System.Reflection.Assembly.GetExecutingAssembly().CodeBase)

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

答案 2 :(得分:12)

Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath

答案 3 :(得分:10)

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

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

示例:

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

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

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

这是我需要的确切行为,而且简单明了。

答案 4 :(得分:5)

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

答案 5 :(得分:0)

您可以写下以下内容:

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