Get path to execution directory of Windows Forms a

2019-01-06 16:16发布

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?

8条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-06 16:29
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
查看更多
小情绪 Triste *
3楼-- · 2019-01-06 16:34

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

查看更多
走好不送
4楼-- · 2019-01-06 16:40

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

查看更多
做个烂人
5楼-- · 2019-01-06 16:42

This could help;

Path.GetDirectoryName(Application.ExecutablePath);

also here is the reference

查看更多
老娘就宠你
6楼-- · 2019-01-06 16:43
string apppath = 
    (new System.IO.FileInfo
    (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
查看更多
女痞
7楼-- · 2019-01-06 16:47

In VB.NET

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

In C#

string directory = AppDomain.CurrentDomain.BaseDirectory;
查看更多
登录 后发表回答