Click here to Skip to main content
14,448,728 members
Rate this:
Please Sign up or sign in to 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 20: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);
Rate this:
Please Sign up or sign in to vote.

Solution 2

   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   
Comments
   
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 :)
Rate this:
Please Sign up or sign in to vote.

Solution 4

Here is the most universal way:

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
   
Comments
Wendelius 5-Sep-11 3:07am
   
Good addition, my 5
   
Thank you, Mika.
--SA
Ali-19 6-Feb-12 5:45am
   
Thnx alot
Rate this:
Please Sign up or sign in to vote.

Solution 5

string path= Application.StartupPath

for get your start up application folder location
   
Comments
Ali-19 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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100