Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, I want to get my application path after I install it, by C# code.
How can I get that? Should I use registry key?
Posted
Updated 4-Sep-11 19:45pm
v2
Comments
M_Mogharrabi 10-Sep-11 1:05am    
Thanks all, but none of these solutions solved my problem.I found the code,i used registry:

using Microsoft.Win32;

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Installer\Assemblies");
string regfilepath="";
if (key!=null) // Make sure there are Assemblies
{
foreach(string Keyname in key.GetSubKeyNames())
{
if (Keyname.IndexOf("YOUR_EXE_FILE.EXE")>0)
{
regfilepath=Keyname.Replace('|','\\');
break;
}
}
}
Michi Elsno 29-Dec-19 15:55pm    
A little late but hey. I'm working on a Windows CE .NET CF2.0 project for my Caska car unit and found the following c# code to work:
string adres = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
MessageBox.Show(adres);

For the current assembly you can use:
System.Reflection.Assembly.GetExecutingAssembly().Location

See: Assembly.Location Property[^].

Depending on the situation (your application structure) you may want to find out the location of the entry assembly. In that case use:
System.Reflection.Assembly.GetEntryAssembly().Location
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 3:04am    
This is the only universal and reliable way I know. All other create problem in different cases. My 5.
A bit more detail in my solution -- please see.
--SA
Wendelius 5-Sep-11 3:07am    
Thank you :)
Here is the most universal way:

C#
string exePath =
    System.IO.Path.GetDirectoryName(
       System.Reflection.Assembly.GetEntryAssembly().Location);


Other methods either require a library which you don't use (such as System.Windows.Forms) or depends on how the code is hosted (for example, using parameters of Main is not reliable).

—SA
 
Share this answer
 
Comments
Wendelius 5-Sep-11 3:07am    
Good addition, my 5
Sergey Alexandrovich Kryukov 5-Sep-11 10:57am    
Thank you, Mika.
--SA
Zeeshan-Ali89 6-Feb-12 5:45am    
Thnx alot
Michael Haephrati 5-Sep-22 15:50pm    
Will the path include the "\" at the end?
 
Share this answer
 
v2
string path= Application.StartupPath

for get your start up application folder location
 
Share this answer
 
Comments
Zeeshan-Ali89 6-Feb-12 6:07am    
The Problem I am facing is that I have created a folder in bin named Image, where I am saving images, but when I open dialog for selecting image so its location is changed and when i save it then its giving error, plz tell me how can i reset the default location

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