如何获取.NET应用程序的路径(而不是启动应用程序的应用程序的路径)

Six*_*ree 2 .net c# vb.net

我无法在运行时获取应用程序的可执行文件的路径.此应用程序由另一个可执行文件启动,每当我尝试使用Application.ExecutablePath时,我都会获得该应用程序的路径,而不是我的.

Dim aPath1 As String = Application.ExecutablePath()
Dim aPath2 As String = Application.StartupPath()
Run Code Online (Sandbox Code Playgroud)

在C#中它会是

string aPath1 = Application.ExecutablePath;
string aPath2 = Application.StartupPath;
Run Code Online (Sandbox Code Playgroud)

这些都没有给我这个程序的路径,其中这两行被执行,它们只告诉程序启动了什么程序.

非常感谢任何帮助,谢谢你提前.

jle*_*lew 5

AppDomain.CurrentDomain.BaseDirectory 在大多数情况下,你会得到你想要的东西.

或者,这将为您提供执行代码的程序集的位置:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);