C# 在 .NET 中获取正在执行的 exe 路径的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222190/
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
What is the best way to get the executing exe's path in .NET?
提问by pistacchio
From program a.exe located in c:/dir I need to open text file c:/dir/text.txt. I don't know where a.exe could be located, but text.txt will always be in the same path. How to get the name of the currently executing assembly from within to program itself so that i can access the text file?
从位于 c:/dir 的程序 a.exe 我需要打开文本文件 c:/dir/text.txt。我不知道 a.exe 可能位于何处,但 text.txt 将始终位于同一路径中。如何从内部获取当前正在执行的程序集的名称以自行编程,以便我可以访问文本文件?
EDIT: what if a.exe is a Windows service? It doesn't have Application as it is not a Windows Applicaion.
编辑:如果 a.exe 是 Windows 服务怎么办?它没有应用程序,因为它不是 Windows 应用程序。
Thanks in advance.
提前致谢。
采纳答案by Tim S. Van Haren
I usually access the directory that contains my application's .exe with:
我通常使用以下命令访问包含我的应用程序的 .exe 的目录:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
回答by Winston Smith
string exePath = Application.ExecutablePath;
string startupPath = Application.StartupPath;
EDIT - Without using application object:
编辑 - 不使用应用程序对象:
string path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
See here for more info:
请参阅此处了解更多信息:
回答by Tommy Carlier
回答by peSHIr
Get the assembly you are interested in (eg. assigned to a System.Reflection.Assembly a
variable):
获取您感兴趣的程序集(例如分配给System.Reflection.Assembly a
变量):
System.Reflection.Assembly.GetEntryAssembly()
, ortypeof(X).Assembly
for a classX
that's in the assembly you're interested in (for Windows Forms you could usetypeof(Program)
)
System.Reflection.Assembly.GetEntryAssembly()
, 或者typeof(X).Assembly
对于X
您感兴趣的程序集中的类(对于 Windows 窗体,您可以使用typeof(Program)
)
Then get the path of where the file from which that assembly a
was loaded from:
然后获取从中a
加载该程序集的文件的路径:
System.IO.Path.GetDirectoryName(a.Location)
System.IO.Path.GetDirectoryName(a.Location)
The Application
object from a Windows Forms application is also a possibility, as explained in other answers.
Application
来自 Windows 窗体应用程序的对象也是一种可能性,如其他答案中所述。
回答by jay_t55
MessageBox.Show("This program is located in: " + Environment.CurrentDirectory);
回答by dynamiclynk
using peSHlr's answer worked well when testing in NUnit as well.
在 NUnit 中进行测试时,使用 peSHlr 的答案也很有效。
var thisType = typeof(MyCustomClass);
var codeLocation = Path.GetDirectoryName(thisType.Assembly.Location);
var codeLocationPath = Path.GetDirectoryName(codeLocation);
var appConfigPath = Path.Combine(codeLocationPath, "AppConfig");
回答by maks
In VB.NET we can get it in following way:
在 VB.NET 中,我们可以通过以下方式获得它:
Assembly.GetEntryAssembly.Location
In C#:
在 C# 中:
Assembly.GetEntryAssembly().Location