Get path to execution directory of Windows Forms application

.NetWinformsPathDirectory

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

In VB.NET

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

In C#

string directory = AppDomain.CurrentDomain.BaseDirectory;

Solution 2 - .Net

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

Solution 3 - .Net

This could help;

Path.GetDirectoryName(Application.ExecutablePath);

also here is the reference

Solution 4 - .Net

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

Solution 5 - .Net

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)

Solution 6 - .Net

string apppath = 
    (new System.IO.FileInfo
    (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;

Solution 7 - .Net

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

Solution 8 - .Net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYaakov EllisView Question on Stackoverflow
Solution 1 - .NetTomas PajonkView Answer on Stackoverflow
Solution 2 - .NetthmsnView Answer on Stackoverflow
Solution 3 - .NetAli ErsözView Answer on Stackoverflow
Solution 4 - .NetBrijesh KannaujiaView Answer on Stackoverflow
Solution 5 - .Netали View Answer on Stackoverflow
Solution 6 - .NetMusiGenesisView Answer on Stackoverflow
Solution 7 - .Netsandeep kView Answer on Stackoverflow
Solution 8 - .Netvignesh pethaperumalView Answer on Stackoverflow