简体   繁体   English

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

[英]How do I get my .NET application's path (not the path of the application that started my application)

I am having trouble getting the path of my application's executable file at runtime. 我无法在运行时获取应用程序的可执行文件的路径。 This application is being started by another executable and whenever I try to use Application.ExecutablePath I get the path to that application instead of mine. 此应用程序由另一个可执行文件启动,每当我尝试使用Application.ExecutablePath时,我都会获得该应用程序的路径,而不是我的。

Dim aPath1 As String = Application.ExecutablePath()
Dim aPath2 As String = Application.StartupPath()

in C# it would be 在C#中它会是

string aPath1 = Application.ExecutablePath;
string aPath2 = Application.StartupPath;

Neither of these give me the path of the program in which those two lines are exectured, they only tell what program started this program. 这些都没有给我这个程序的路径,其中这两行被执行,它们只告诉程序启动了什么程序。

Any help would be greatly appreciated, Thank you in advance. 非常感谢任何帮助,谢谢你提前。

AppDomain.CurrentDomain.BaseDirectory will get you what you want in most cases. AppDomain.CurrentDomain.BaseDirectory可以在大多数情况下为您提供所需的内容。

Alternatively, this will get you the location of the assembly which is executing the code: 或者,这将为您提供执行代码的程序集的位置:

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

尝试

Assembly.GetExecutingAssembly().Location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM