【问题标题】:Get path to execution directory of Windows Forms application获取 Windows 窗体应用程序执行目录的路径
【发布时间】:2010-09-22 16:20:12
【问题描述】:

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

有谁知道 .NET 中的内置方法可以做到这一点?

【问题讨论】:

标签: .net winforms path directory


【解决方案1】:

在 VB.NET 中

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

在 C# 中

string directory = AppDomain.CurrentDomain.BaseDirectory;

【讨论】:

  • C# 位置也适用于 VB 或其他不支持“我的”命名空间的语言。
  • @Tomas Pajonk,我可以建议将 c#“文件名”变量更改为“目录”吗?
【解决方案2】:

这会有所帮助;

Path.GetDirectoryName(Application.ExecutablePath);

这里也是reference

【讨论】:

    【解决方案3】:

    Application.Current 产生一个 appdomain http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx

    这也应该给你装配的位置

    AppDomain.CurrentDomain.BaseDirectory
    

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

    【讨论】:

    • 应该是Application.CurrentDomain。
    • 这在 .NET 4.0 中似乎不起作用,但 Application.UserAppDataPath 确实起作用。
    • AppDomain.CurrentDomain.BaseDirectory
    【解决方案4】:
    string apppath = 
        (new System.IO.FileInfo
        (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
    

    【讨论】:

    • 抱歉,Assembly 命名空间和代码显示面板不能很好地配合使用。我讨厌滚动条。
    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

      看看这个:

      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
      

      【讨论】:

        【解决方案7】:

        这两个示例都在 VB.NET 中。

        调试路径:

        TextBox1.Text = My.Application.Info.DirectoryPath
        

        EXE路径:

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

        【讨论】:

          【解决方案8】:
          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
          

          【讨论】:

            猜你喜欢
            • 2011-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-08
            • 2011-05-20
            • 1970-01-01
            • 2016-06-15
            • 1970-01-01
            相关资源
            最近更新 更多