C# 如何找到当前的可执行文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/980202/
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-08-06 04:45:45  来源:igfitidea点击:

How do I find the current executable filename?

c#.net

提问by Boris Callens

Possible Duplicate:
How do I get the name of the current executable in C#?

可能的重复:
如何在 C# 中获取当前可执行文件的名称?

An executable file loads an external library.
Is there a way for the library to know the calling executable file?

可执行文件加载外部库。
库有没有办法知道调用的可执行文件?

(I would have sworn I saw the answer to this elsewhere, but I can't seem to find it anymore)

(我发誓我在别处看到了这个答案,但我似乎再也找不到了)

采纳答案by Thomas Levesque

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

回答by Noldorin

I think this should be what you want:

我认为这应该是你想要的:

System.Reflection.Assembly.GetEntryAssembly().Location

This returns the assembly that was first loaded when the process started up, which would seem to be what you want.

这将返回进程启动时首次加载的程序集,这似乎是您想要的。

GetCallingAssemblywon't necessarily return the assembly you want in the general case, since it returns the assembly containing the method immediately higher in the call stack (i.e. it could be in the same DLL).

GetCallingAssembly在一般情况下,不一定会返回您想要的程序集,因为它返回包含调用堆栈中较高位置的方法的程序集(即它可能在同一个 DLL 中)。

回答by Anton Tykhyy

Assembly.GetEntryAssembly()

Assembly.GetEntryAssembly()

回答by Matt Brindley

If you want the executable:

如果你想要可执行文件:

System.Reflection.Assembly.GetEntryAssembly().Location

If you want the assembly that's consuming your library (which could be the same assembly as above, if your code is called directly from a class within your executable):

如果您想要使用您的库的程序集(如果您的代码直接从可执行文件中的类调用,则该程序集可能与上述程序集相同):

System.Reflection.Assembly.GetCallingAssembly().Location

If you'd like just the filenameand not the path, use:

如果您只想要文件而不是路径,请使用:

Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location)

回答by Alexander Zwitbaum

In addition to the answers above.

除了上面的答案。

I wrote following test.exe as console application

我写了以下 test.exe 作为控制台应用程序

static void Main(string[] args) {
  Console.WriteLine(
    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
  Console.WriteLine(
    System.Reflection.Assembly.GetEntryAssembly().Location);
  Console.WriteLine(
    System.Reflection.Assembly.GetExecutingAssembly().Location);
  Console.WriteLine(
    System.Reflection.Assembly.GetCallingAssembly().Location);
}

Then I compiled the project and renamed its output to the test2.exe file. The output lines were correct and the same.

然后我编译了该项目并将其输出重命名为 test2.exe 文件。输出行正确且相同。

But, if I start it in the Visual Studio, the result is:

但是,如果我在 Visual Studio 中启动它,结果是:

d:\test2.vhost.exe

d:\test2.exe

d:\test2.exe

C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll

d:\test2.vhost.exe

d:\test2.exe

d:\test2.exe

C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll

The ReSharper plug-in to the Visual Studio has underlined the

Visual Studio 的 ReSharper 插件强调了

System.Diagnostics.Process.GetCurrentProcess().MainModule

as possible System.NullReferenceException. If you look into documentation of the MainModule you will find that this property can throw also NotSupportedException, PlatformNotSupportedException and InvalidOperationException.

尽可能 System.NullReferenceException。如果您查看 MainModule 的文档,您会发现该属性也可以抛出 NotSupportedException、PlatformNotSupportedException 和 InvalidOperationException。

The GetEntryAssembly method is also not 100% "safe". MSDN:

GetEntryAssembly 方法也不是 100%“安全”的。微软:

The GetEntryAssembly method can return null when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.

当从非托管应用程序加载托管程序集时,GetEntryAssembly 方法可以返回 null。例如,如果非托管应用程序创建了一个用 C# 编写的 COM 组件的实例,则从 C# 组件调用 GetEntryAssembly 方法将返回 null,因为进程的入口点是非托管代码而不是托管程序集。

For my solutions, I prefer the Assembly.GetEntryAssembly().Location.

对于我的解决方案,我更喜欢Assembly.GetEntryAssembly().Location.

More interest is if need to solve the problem for the virtualization. For example, we have a project, where we use a Xenocode Postbuild to link the .net code into one executable. This executable must be renamed. So all the methods above didn't work, because they only gets the information for the original assembly or inner process.

更感兴趣的是是否需要为虚拟化解决问题。例如,我们有一个项目,我们使用 Xenocode Postbuild 将 .net 代码链接到一个可执行文件中。必须重命名此可执行文件。所以上面的所有方法都不起作用,因为它们只获取原始组件或内部过程的信息。

The only solution I found is

我找到的唯一解决方案是

var location = System.Reflection.Assembly.GetEntryAssembly().Location;
var directory = System.IO.Path.GetDirectoryName(location);
var file = System.IO.Path.Combine(directory, 
  System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe");

回答by jp2code

This one was not included:

这一项没有包括在内:

System.Windows.Forms.Application.ExecutablePath;

~Joe

~乔

回答by mnaoumov

Environment.GetCommandLineArgs()[0]

Environment.GetCommandLineArgs()[0]