60

I would like to get the path to the execution directory of a Windows Forms application. (That is, the directory in which the executable is located.)

Does anyone know of a built-in method in .NET to do this?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
  • Does this answer your question? [Best way to get application folder path](https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – Michael Freidgeim Oct 01 '20 at 02:24

8 Answers8

68

In VB.NET

Dim directory as String = My.Application.Info.DirectoryPath

In C#

string directory = AppDomain.CurrentDomain.BaseDirectory;
Tomas Pajonk
  • 5,132
  • 8
  • 41
  • 51
59

Application.Current results in an appdomain http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx

Also this should give you the location of the assembly

AppDomain.CurrentDomain.BaseDirectory

I seem to recall there being multiple ways of getting the location of the application. but this one worked for me in the past atleast (it's been a while since i've done winforms programming :/)

Luke
  • 1,916
  • 1
  • 15
  • 15
thmsn
  • 1,976
  • 1
  • 18
  • 25
19

This could help;

Path.GetDirectoryName(Application.ExecutablePath);

also here is the reference

Ali Ersöz
  • 15,860
  • 11
  • 50
  • 64
9

System.Windows.Forms.Application.StartupPath will solve your problem, I think

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
2

Both of the examples are in VB.NET.

Debug path:

TextBox1.Text = My.Application.Info.DirectoryPath

EXE path:

TextBox2.Text = IO.Path.GetFullPath(Application.ExecutablePath)
Alex
  • 1,549
  • 2
  • 16
  • 41
али
  • 21
  • 1
1
string apppath = 
    (new System.IO.FileInfo
    (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
1

Check this out:

Imports System.IO
Imports System.Management

Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Path.GetFullPath(Application.ExecutablePath)
        Process.Start(TextBox1.Text)
    End Sub
End Class
Jimi
  • 29,621
  • 8
  • 43
  • 61
sandeep k
  • 11
  • 1
0
Private Sub Main_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    Dim args() As String = Environment.GetCommandLineArgs()
    If args.Length > 0 Then
        TextBox1.Text = Path.GetFullPath(Application.ExecutablePath)
        Process.Start(TextBox1.Text)   
    End If
End Sub
Jimi
  • 29,621
  • 8
  • 43
  • 61