Linda
Linda

Reputation: 273

Get program path in VB.NET?

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

Upvotes: 23

Views: 178909

Answers (8)

Alireza Nemeti
Alireza Nemeti

Reputation: 162

If the path is a drive, a slash will also appear in the path, and this time the use will cause problems. To unify, the best solution is the following command.

        Dim FileName As String = "MyFileName"
    Dim MyPath1 As String = Application.StartupPath().TrimEnd("\") & "\" & FileName
    Dim MyPath2 As String = My.Application.Info.DirectoryPath.TrimEnd("\") & "\" & FileName

Upvotes: 1

Wajid Ali Khan
Wajid Ali Khan

Reputation: 1

Set Your Own application Path

Dim myPathsValues As String

    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"

Upvotes: 0

Eddy Jawed
Eddy Jawed

Reputation: 457

You can also use:

Dim strPath As String = AppDomain.CurrentDomain.BaseDirectory

Upvotes: 5

KHALID
KHALID

Reputation: 87

You can get path by this code

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

Upvotes: 3

Mark Hurd
Mark Hurd

Reputation: 10931

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

This is using the My feature 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.

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.

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

Upvotes: 25

Corey B
Corey B

Reputation: 15

I use:

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

Upvotes: -3

lee-m
lee-m

Reputation: 2267

For a console application you can use System.Reflection.Assembly.GetExecutingAssembly().Location as 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.

Upvotes: 4

Nick Craver
Nick Craver

Reputation: 630399

For that you can use the Application object.

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

Dim appPath As String = Application.StartupPath()

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

Dim exePath As String = Application.ExecutablePath()

Upvotes: 39

Related Questions