Get path to execution directory of Windows Forms application

前端 未结 8 619
一个人的身影
一个人的身影 2020-11-29 04:47

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

相关标签:
8条回答
  • 2020-11-29 05:26
    string apppath = 
        (new System.IO.FileInfo
        (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
    
    0 讨论(0)
  • 2020-11-29 05:29

    In VB.NET

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

    In C#

    string directory = AppDomain.CurrentDomain.BaseDirectory;
    
    0 讨论(0)
  • 2020-11-29 05:33
    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
    
    0 讨论(0)
  • 2020-11-29 05:35

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

    0 讨论(0)
  • 2020-11-29 05:38

    This could help;

    Path.GetDirectoryName(Application.ExecutablePath);
    

    also here is the reference

    0 讨论(0)
  • 2020-11-29 05:40

    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 讨论(0)
提交回复
热议问题