获取可执行文件的绝对路径,使用C#? [英] Getting the absolute path of the executable, using 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屋!