找到.exe目录? [英] find the .exe directory?

查看:121
本文介绍了找到.exe目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到.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

推荐答案

这就是条目程序集的主要可执行模块的位置:
This is such thing as the location of a main executable module of the entry assembly:
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;



当然,您可以获取任何给定程序集的位置,例如调用程序集的位置.严格来说,程序集不必是单个文件,也可以在不同的模块中实现,但是Visual Studio仅支持创建单模块程序集(引用的程序集不计在内,它们是不同的程序集).在所有情况下,汇编位置都是其主要可执行模块的位置.请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ].

还有许多其他方法,但是其中一些方法需要其他引用,而某些方法根据应用程序的托管方式给出不同的结果.这种方法是可靠的方法.

—SA



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.

—SA


属性Application.ExecutablePath为您提供exe的完整路径.参见此代码;
The Property Application.ExecutablePath gives you the full path of the exe. See this code;
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目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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