我想获取 Windows 窗体应用程序的执行目录的路径。(即可执行文件所在的目录。)
有谁知道.NET 中的内置方法可以做到这一点?
在 VB.NET 中
Dim directory as String = My.Application.Info.DirectoryPath
在 C# 中
string directory = AppDomain.CurrentDomain.BaseDirectory;
Application.Current 导致 appdomain http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx
这也应该给你装配的位置
AppDomain.CurrentDomain.BaseDirectory
我似乎记得有多种方法可以获取应用程序的位置。但这至少在过去对我有用(我已经有一段时间没有做过winforms编程了:/)
System.Windows.Forms.Application.StartupPath
会解决你的问题,我想
这两个示例都在 VB.NET 中。
调试路径:
TextBox1.Text = My.Application.Info.DirectoryPath
EXE路径:
TextBox2.Text = IO.Path.GetFullPath(Application.ExecutablePath)
string apppath =
(new System.IO.FileInfo
(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
看一下这个:
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
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