61

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?

1

8 Answers 8

70

In VB.NET

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

In C#

string directory = AppDomain.CurrentDomain.BaseDirectory;
2
  • 4
    The C# location will also work in VB or other languages that don't support the "My" namepsace. Commented Nov 17, 2008 at 14:53
  • @Tomas Pajonk, may I suggest changing the c# "FileName" variable to "directory"?
    – grenade
    Commented Apr 21, 2010 at 9:32
60

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

3
  • 2
    It should be Application.CurrentDomain.
    – Max
    Commented Apr 20, 2011 at 15:08
  • 1
    This did not appear to work in .NET 4.0, but Application.UserAppDataPath did work. Commented Feb 20, 2014 at 14:22
  • 1
    AppDomain.CurrentDomain.BaseDirectory
    – A Khudairy
    Commented Jan 19, 2016 at 8:17
19

This could help;

Path.GetDirectoryName(Application.ExecutablePath);

also here is the reference

9

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

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)
1
string apppath = 
    (new System.IO.FileInfo
    (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
1
  • Sorry, the Assembly namespace and the code display panel do not play nicely together. I hate scrollbars. Commented Nov 17, 2008 at 14:51
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
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.