如何在 C# 中获取当前可执行文件的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/616584/
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
How do I get the name of the current executable in C#?
提问by Joakim
I want to get the name of the currently running program, that is the executable name of the program. In C/C++ you get it from args[0]
.
我想获取当前正在运行的程序的名称,即程序的可执行名称。在 C/C++ 中,您可以从args[0]
.
采纳答案by Steven A. Lowe
System.AppDomain.CurrentDomain.FriendlyName
回答by Andrew Hare
Try this:
尝试这个:
System.Reflection.Assembly.GetExecutingAssembly()
This returns you a System.Reflection.Assembly
instance that has all the data you could ever want to know about the current application. I think that the Location
property might get what you are after specifically.
这将返回一个System.Reflection.Assembly
实例,其中包含您可能想知道的有关当前应用程序的所有数据。我认为该Location
物业可能会得到您特别想要的东西。
回答by James B
This should suffice:
这应该足够了:
Environment.GetCommandLineArgs()[0];
回答by Frederik Gheysels
Is this what you want:
这是你想要的吗:
Assembly.GetExecutingAssembly ().Location
回答by Aaron Daniels
System.Diagnostics.Process.GetCurrentProcess()
gets the currently running process. You can use the ProcessName
property to figure out the name. Below is a sample console app.
System.Diagnostics.Process.GetCurrentProcess()
获取当前运行的进程。您可以使用该ProcessName
属性来确定名称。下面是一个示例控制台应用程序。
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Process.GetCurrentProcess().ProcessName);
Console.ReadLine();
}
}
回答by Jeff Yates
You can use Environment.GetCommandLineArgs()
to obtain the arguments and Environment.CommandLine
to obtain the actual command line as entered.
您可以使用Environment.GetCommandLineArgs()
获取参数并Environment.CommandLine
获取输入的实际命令行。
Also, you can use Assembly.GetEntryAssembly()
or Process.GetCurrentProcess()
.
此外,您可以使用Assembly.GetEntryAssembly()
或Process.GetCurrentProcess()
。
However, when debugging, you should be careful as this final example may give your debugger's executable name (depending on how you attach the debugger) rather than your executable, as may the other examples.
但是,在调试时,您应该小心,因为最后一个示例可能会提供您的调试器的可执行文件名称(取决于您附加调试器的方式),而不是您的可执行文件,其他示例也是如此。
回答by langpavel
System.Reflection.Assembly.GetEntryAssembly().Location
returns location of exe name if assembly is not loaded from memory.System.Reflection.Assembly.GetEntryAssembly().CodeBase
returns location as URL.
System.Reflection.Assembly.GetEntryAssembly().Location
如果程序集未从内存中加载,则返回 exe 名称的位置。System.Reflection.Assembly.GetEntryAssembly().CodeBase
将位置作为 URL 返回。
回答by JohnB
Couple more options:
还有更多选择:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
回答by Lee Grissom
System.AppDomain.CurrentDomain.FriendlyName
- Returns the filename with extension (e.g. MyApp.exe).
System.AppDomain.CurrentDomain.FriendlyName
- 返回带有扩展名的文件名(例如 MyApp.exe)。
System.Diagnostics.Process.GetCurrentProcess().ProcessName
- Returns the filename withoutextension (e.g. MyApp).
System.Diagnostics.Process.GetCurrentProcess().ProcessName
- 返回不带扩展名的文件名(例如 MyApp)。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
- Returns the full path and filename (e.g. C:\Examples\Processes\MyApp.exe). You could then pass this into System.IO.Path.GetFileName()
or System.IO.Path.GetFileNameWithoutExtension()
to achieve the same results as the above.
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
- 返回完整路径和文件名(例如 C:\Examples\Processes\MyApp.exe)。然后,您可以将其传递给System.IO.Path.GetFileName()
orSystem.IO.Path.GetFileNameWithoutExtension()
以实现与上述相同的结果。
回答by Orwellophile
When uncertain or in doubt, run in circles, scream and shout.
当不确定或有疑问时,绕圈跑,尖叫和喊叫。
class Ourself
{
public static string OurFileName() {
System.Reflection.Assembly _objParentAssembly;
if (System.Reflection.Assembly.GetEntryAssembly() == null)
_objParentAssembly = System.Reflection.Assembly.GetCallingAssembly();
else
_objParentAssembly = System.Reflection.Assembly.GetEntryAssembly();
if (_objParentAssembly.CodeBase.StartsWith("http://"))
throw new System.IO.IOException("Deployed from URL");
if (System.IO.File.Exists(_objParentAssembly.Location))
return _objParentAssembly.Location;
if (System.IO.File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName))
return System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName;
if (System.IO.File.Exists(System.Reflection.Assembly.GetExecutingAssembly().Location))
return System.Reflection.Assembly.GetExecutingAssembly().Location;
throw new System.IO.IOException("Assembly not found");
}
}
I can't claim to have tested each option, but it doesn't do anything stupid like returning the vhost during debugging sessions.
我不能声称已经测试了每个选项,但它不会做任何愚蠢的事情,例如在调试会话期间返回 vhost。