Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How would I find an .exe file's full path?
For example,
RunProgram.exe would be in
C:\\Program Files\\somewhere\\somefolder


so I want to search for "RunProgram.exe"
and return
C:\\Program Files\\somewhere\\somefolder\\RunProgram.exe
Posted

This is such thing as the location of a main executable module of the entry assembly:
C#
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;


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
 
Share this answer
 
Comments
codeBegin 6-Jun-12 7:05am    
Good explanation my 5
Sergey Alexandrovich Kryukov 6-Jun-12 11:27am    
Thank you.
--SA
Maciej Los 6-Jun-12 18:14pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 6-Jun-12 18:25pm    
Thank you, Maciej.
--SA
The Property Application.ExecutablePath gives you the full path of the exe. See this code;
C#
MessageBox.Show(Application.ExecutablePath);

Please note that this only works when using a WinForms application.

You may also get the information from the current process as below:
C#
MessageBox.Show(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-12 1:04am    
This is not a good solution at all, because not all applications has the Application. Please see my answer.
--SA
Maciej Los 6-Jun-12 18:15pm    
I agree with you, SA. That's the reason of vote 4.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900