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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/295687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:37:52  来源:igfitidea点击:

Get path to execution directory of Windows Forms application

.netwinformspathdirectory

提问by Yaakov Ellis

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.)

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

Does anyone know of a built-in method in .NET to do this?

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

回答by Tomas Pajonk

In VB.NET

在 VB.NET 中

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

In C#

在 C# 中

string directory = AppDomain.CurrentDomain.BaseDirectory;

回答by thmsn

Application.Current results in an appdomain http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx

Application.Current 结果在一个 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 :/)

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

回答by Ali Ers?z

This could help;

这可能会有所帮助;

Path.GetDirectoryName(Application.ExecutablePath);

also here is the reference

这里也是参考

回答by Ali Ers?z

System.Windows.Forms.Application.StartupPathwill solve your problem, I think

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

回答by али

Both of the examples are in VB.NET.

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

Debug path:

调试路径:

TextBox1.Text = My.Application.Info.DirectoryPath

EXE path:

EXE路径:

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

回答by sandeep k

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

回答by MusiGenesis

string apppath = 
    (new System.IO.FileInfo
    (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;

回答by vignesh pethaperumal

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