Warm tip: This article is reproduced from serverfault.com, please click

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

(directory - Getting the absolute path of the executable, using C#?)

发布于 2009-11-01 22:01:00

看一下这个伪代码:

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

如果我构建上述程序并将可执行文件放在中C:/meow/This executable is located in C:/meow/则无论当前工作目录如何,每次运行时它都会打印出

我如何轻松地使用来完成此操作C#

Questioner
user12163
Viewed
0
Mark Rushakoff 2009-11-02 06:03:32

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

或者,缩写Application.ExecutablePath为“获取启动应用程序的可执行文件的路径,包括可执行文件的名称”,这可能意味着它的可靠性稍差,具体取决于应用程序的启动方式。