使用 C# 获取可执行文件的绝对路径? [英] Getting the absolute path of the executable, using C#?

查看:18
本文介绍了使用 C# 获取可执行文件的绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个伪代码:

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

如果我构建上面的程序并将可执行文件放在C:/meow/,它每次都会打印出这个可执行文件位于C:/meow/无论当前工作目录如何,它都会运行.

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# 轻松完成此操作?

How could I easily accomplish this using C#?

推荐答案

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

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

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

Or, there's the shorter Application.ExecutablePath which "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.

这篇关于使用 C# 获取可执行文件的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆