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

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

问题描述

看看这个伪代码:

  string exe_path = system.get_exe_path()
print这个可执行文件位于+ exe_path

如果我构建上述程序并将可执行文件放在$ code> C:/ meow / ,它将打印出每次运行时,这个可执行文件位于C:/ meow / 中,不管目前的工作目录。



如何轻松地使用 C#

解决方案

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



或者,较短的 Application.ExecutablePath 其中获取启动应用程序的可执行文件的路径,包括可执行文件名称,这样可能意味着它的可靠性略低于应用程序的启动方式。


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.

How could I easily accomplish this using C#?

解决方案

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.

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天全站免登陆