.net 获取Windows窗体应用程序的执行目录的路径

.net 获取Windows窗体应用程序的执行目录的路径,.net,winforms,path,directory,.net,Winforms,Path,Directory,我想获取Windows窗体应用程序的执行目录的路径。(即,可执行文件所在的目录。) 有人知道在.NET中有内置的方法来实现这一点吗?在VB.NET中 Dim directory as String = My.Application.Info.DirectoryPath 在C中# 这可能会有所帮助 Path.GetDirectoryName(Application.ExecutablePath); 这里还有应用程序。appdomain中的当前结果 此外,这将为您提供程序集的位置 AppDom

我想获取Windows窗体应用程序的执行目录的路径。(即,可执行文件所在的目录。)

有人知道在.NET中有内置的方法来实现这一点吗?

在VB.NET中

Dim directory as String = My.Application.Info.DirectoryPath
在C中#

这可能会有所帮助

Path.GetDirectoryName(Application.ExecutablePath);

这里还有应用程序。appdomain中的当前结果

此外,这将为您提供程序集的位置

AppDomain.CurrentDomain.BaseDirectory

我似乎记得有多种方法可以获取应用程序的位置。但是这个至少在过去对我有用(我已经有一段时间没有做winforms编程了:/)

System.Windows.Forms.Application.StartupPath
会解决你的问题,我想看看这个:

Imports System.IO
导入系统。管理
公开课表格1
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
TextBox1.Text=Path.GetFullPath(Application.ExecutablePath)
Process.Start(TextBox1.Text)
端接头
末级

这两个示例都在VB.NET中

调试路径:

TextBox1.Text = My.Application.Info.DirectoryPath
EXE路径:

TextBox2.Text = IO.Path.GetFullPath(Application.ExecutablePath)

很抱歉,程序集命名空间和代码显示面板不能很好地配合使用。我讨厌滚动条。C#位置也可以在VB或其他不支持“我的”名称的语言中使用。@Tomas Pajonk,我可以建议将C#“FileName”变量更改为“directory”?它应该是Application.CurrentDomain。这在.NET 4.0中似乎不起作用,但是Application.UserAppDataPath确实有效。AppDomain.CurrentDomain.BaseDirectory这是否回答了您的问题?
TextBox1.Text = My.Application.Info.DirectoryPath
TextBox2.Text = IO.Path.GetFullPath(Application.ExecutablePath)
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