使用 C# 获取可执行文件的绝对路径?

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

Getting the absolute path of the executable, using C#?

c#directoryexecutable

提问by

Have a look at this pseudocode:

看看这个伪代码:

string exe_path = system.get_exe_path()
print "This executable is located in " + exe_path

If I build the above program and place the executable in C:/meow/, It would print out This executable is located in C:/meow/each time it is run, regardless of the current working directory.

如果我建立了上述程序,并将可执行的C:/meow/,它会打印出This executable is located in C:/meow/每次运行时,无论当前工作目录。

How could I easily accomplish this using C#?

我怎么能用 轻松完成这个C#

采纳答案by Mark Rushakoff

MSDN has an articlethat says to use System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; if you need the directory, use System.IO.Path.GetDirectoryNameon that result.

MSDN 有一篇文章说要使用System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;如果您需要该目录,请System.IO.Path.GetDirectoryName在该结果上使用。

Or, there's the shorter Application.ExecutablePathwhich "Gets the path for the executable file that started the application, including the executable name" so that might mean it's slightly less reliable depending on how the application was launched.

或者,有更短的Application.ExecutablePath“获取启动应用程序的可执行文件的路径,包括可执行文件名称”,因此这可能意味着根据应用程序的启动方式,它的可靠性略低。

回答by Mike de Klerk

"Gets the path or UNC location of the loaded file that contains the manifest."

“获取包含清单的加载文件的路径或 UNC 位置。”

See: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx

请参阅:http: //msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx

Application.ResourceAssembly.Location

回答by Liquid Core

AppDomain.CurrentDomain.BaseDirectory

回答by Muhammad Mubashir

Suppose i have .config file in console app and now am getting like below.

假设我在控制台应用程序中有 .config 文件,现在变得如下所示。

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\YourFolderName\log4net.config";

回答by newb

using System.Reflection;

string myExeDir = new FileInfo(Assembly.GetEntryAssembly().Location).Directory.ToString();

回答by user1

var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

I jumped in for the top rated answer and found myself not getting what I expected. I had to read the comments to find what I was looking for.

我跳入评分最高的答案,发现自己没有得到预期的结果。我必须阅读评论才能找到我要找的东西。

For that reason I am posting the answer listed in the comments to give it the exposure it deserves.

出于这个原因,我发布了评论中列出的答案,以给予它应有的曝光度。

回答by Sébastien

On my side, I used, with a form application:

在我这边,我使用了一个表单应用程序:

String Directory = System.Windows.Forms.Application.StartupPath;

it takes the application startup path.

它采用应用程序启动路径。