问题描述
如何找到.exe文件的完整路径?
例如,
RunProgram.exe将在
How would I find an .exe file''s full path?
For example,
RunProgram.exe would be in
C:\\Program Files\\somewhere\\somefolder
所以我想搜索"RunProgram.exe"
然后返回
so I want to search for "RunProgram.exe"
and return
C:\\Program Files\\somewhere\\somefolder\\RunProgram.exe
推荐答案
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
当然,您可以获取任何给定程序集的位置,例如调用程序集的位置.严格来说,程序集不必是单个文件,也可以在不同的模块中实现,但是Visual Studio仅支持创建单模块程序集(引用的程序集不计在内,它们是不同的程序集).在所有情况下,汇编位置都是其主要可执行模块的位置.请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ].
还有许多其他方法,但是其中一些方法需要其他引用,而某些方法根据应用程序的托管方式给出不同的结果.这种方法是可靠的方法.
Of course, you can get the location of any given assembly, such as the calling one. Strictly speaking, an assembly does not have to be a single file and may be implemented in different modules, but Visual Studio supports creation of only single-module assemblies (the referenced assemblies are not counted, they are different assemblies). In all cases, the assembly location is the location of its main executable module. Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].
There are a number of other methods, but some of them require additional references, and some gives different results depending on how the application is hosted; this method is the reliable one.
MessageBox.Show(Application.ExecutablePath);
请注意,这仅在使用WinForms应用程序时有效.
您还可以从当前流程中获取信息,如下所示:
Please note that this only works when using a WinForms application.
You may also get the information from the current process as below:
MessageBox.Show(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
这篇关于找到.exe目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!