在 VB.NET 中获取程序路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2216141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:52:20  来源:igfitidea点击:

Get program path in VB.NET?

.netvb.net

提问by Linda

How can I get the absolute path of program I'm running?

如何获得我正在运行的程序的绝对路径?

回答by Nick Craver

For that you can use the Applicationobject.

为此,您可以使用该Application对象。

Startup path, just the folder, use Application.StartupPath()

启动路径,只是文件夹,使用Application.StartupPath()

Dim appPath As String = Application.StartupPath()

Full .exe path, including the program.exe name on the end:, use Application.ExecutablePath()

完整的 .exe 路径,包括末尾的 program.exe 名称:,使用Application.ExecutablePath()

Dim exePath As String = Application.ExecutablePath()

回答by Mark Hurd

Try this: My.Application.Info.DirectoryPath[MSDN]

试试这个:My.Application.Info.DirectoryPath[MSDN]

This is using the Myfeature of VB.NET. This particular property is available for all non-web project types, since .NET Framework 2.0, including Console Apps as you require.

这是使用MyVB.NET的特性。自 .NET Framework 2.0 起,此特定属性可用于所有非 Web 项目类型,包括您需要的控制台应用程序。

As long as you trust Microsoft to continue to keep this working correctly for all the above project types, this is simpler to use than accessing the other "more direct" solutions.

只要您相信 Microsoft 会继续为上述所有项目类型正常工作,这比访问其他“更直接”的解决方案更易于使用。

Dim appPath As String = My.Application.Info.DirectoryPath

回答by lee-m

For a console application you can use System.Reflection.Assembly.GetExecutingAssembly().Locationas long as the call is made within the code of the console app itself, if you call this from within another dll or plugin this will return the location of that DLL and not the executable.

对于控制台应用程序System.Reflection.Assembly.GetExecutingAssembly().Location,只要在控制台应用程序本身的代码中进行调用,您就可以使用它,如果您从另一个 dll 或插件中调用它,这将返回该 DLL 的位置,而不是可执行文件。

回答by Eddy Jawed

You can also use:

您还可以使用:

Dim strPath As String = AppDomain.CurrentDomain.BaseDirectory

回答by KHALID

You can get path by this code

您可以通过此代码获取路径

Dim CurDir as string = My.Application.Info.DirectoryPath

回答by Wajid Ali Khan

Set Your Own application Path

设置您自己的应用程序路径

Dim myPathsValues As String

将 myPathsValues 调暗为字符串

    TextBox1.Text = Application.StartupPath
    TextBox2.Text = Len(Application.StartupPath)
    TextBox3.Text = Microsoft.VisualBasic.Right(Application.StartupPath, 10)
    myPathsValues = Val(TextBox2.Text) - 9
    TextBox4.Text = Microsoft.VisualBasic.Left(Application.StartupPath, myPathsValues) & "Reports"

回答by Corey B

I use:

我用:

Imports System.IO
Dim strPath as String=Directory.GetCurrentDirectory