vb.net 运行应用程序的可执行目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2593458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executable directory where application is running from?
提问by JPJedi
I need to get the path (not the executable) where my application is running from:
我需要获取运行我的应用程序的路径(不是可执行文件):
System.AppDomain.CurrentDomain.BaseDirectory()
When I run the above statement with & "/images/image.jpg" on my local machine it works fine but when I install the application on another machine it says it cannot find the file and there is a lot of extra path information some.
当我在本地机器上用 & "/images/image.jpg" 运行上面的语句时,它工作正常,但是当我在另一台机器上安装应用程序时,它说它找不到文件,并且有很多额外的路径信息。
I just need the directory of where the app is running. I am coding in VB.NET with Visual Studio 2008.
我只需要应用程序运行的目录。我正在使用 Visual Studio 2008 在 VB.NET 中编码。
Thanks!
谢谢!
回答by Derek Ziemba
This is the first post on google so I thought I'd post different ways that are available and how they compare. Unfortunately I can't figure out how to create a table here, so it's an image. The code for each is below the image using fully qualified names.
这是谷歌上的第一篇文章,所以我想我会发布不同的可用方式以及它们的比较方式。不幸的是,我无法弄清楚如何在这里创建表格,所以它是一个图像。每个代码都在使用完全限定名称的图像下方。
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))
回答by Justin Niessner
Dim strPath As String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Taken from HOW TO: Determine the Executing Application's Path (MSDN)
回答by MrCalvin
Dim P As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
P = New Uri(P).LocalPath
回答by Jason Alls
I needed to know this and came here, before I remembered the Environment class.
在我想起环境课程之前,我需要知道这一点并来到这里。
In case anyone else had this issue, just use this: Environment.CurrentDirectory
.
如果其他人有这个问题,只需使用这个:Environment.CurrentDirectory
。
Example:
例子:
Dim dataDirectory As String = String.Format("{0}\Data\", Environment.CurrentDirectory)
When run from Visual Studio in debug mode yeilds:
在调试模式下从 Visual Studio 运行时:
C:\Development\solution folder\application folder\bin\debug
This is the exact behaviour I needed, and its simple and straightforward enough.
这正是我需要的行为,而且它足够简单明了。
回答by Mike Dinescu
You could use the static StartupPathproperty of the Application class.
您可以使用Application 类的静态StartupPath属性。
回答by SLaks
You can write the following:
您可以编写以下内容:
Path.Combine(Path.GetParentDirectory(GetType(MyClass).Assembly.Location), "Images\image.jpg")