Different ways to get Application Executable Path

Different ways to get Application Executable Path

Added by Sixth Sense updated on Tuesday, April 3, 2018

Problem:

Many times we need to know executable path of Application and some time we need to know the path of assembly which is loaded. There are many options to get the location but not all are good as we think.

So be careful and use correct method to get application location based on your needs. Here is small tutorial in which we discuss different methods to get location of executable.

Application.StartupPath:

The path for the executable file that started the application.

Application.ExecutablePath:

The path and executable name for the executable file that started the application.

AppDomain.CurrentDomain.BaseDirectory:

The base directory that the assembly resolver uses to probe for assemblies.

System.IO.Directory.GetCurrentDirectory:

Gets the current working directory of the application.

Environment.CurrentDirectory:

Gets or sets the fully qualified path of the current working directory.

Assembly.Location:

Gets the full path or UNC location of the loaded file that contains the manifest.

Assembly.GetExecutingAssembly().CodeBase:

Gets the location of the assembly as specified originally.

Real World:

Lets test out these options, so that I make a simple Windows Forms Application and add following code and move my exe to c drive.

label1.Text = "Application.StartupPath : " +  Application.StartupPath;
label2.Text = "Application.ExecutablePath : " + Application.ExecutablePath;
label3.Text = "AppDomain.CurrentDomain.BaseDirectory : " + AppDomain.CurrentDomain.BaseDirectory;
label4.Text = "System.IO.Directory.GetCurrentDirectory : " + System.IO.Directory.GetCurrentDirectory();
label5.Text = "Environment.CurrentDirectory : " + Environment.CurrentDirectory;
label6.Text = "Assembly.Location : " + this.GetType().Assembly.Location;
label7.Text = "Assembly.GetExecutingAssembly().CodeBase : " + System.Reflection.Assembly.GetExecutingAssembly().CodeBase;				
				

and see the results below.

text

So every option seems to be correct. But wait what if run this application from other application i.e cmd.exe

text

See the result of System.IO.Directory.GetCurrentDirectory and Environment.CurrentDirectory are different of others.

System.IO.Directory.GetCurrentDirectory		
Environment.CurrentDirectory
				

And what if I load assembly from other location, for that I copy this assembly to D drive and load this form from C drive exe. Now see the results.

text

At last run application from cmd.

text

Conclusion:

If we want to get Running Application Path, and Application is Windows Form application then we can use.

Application.StartupPath
Application.ExecutablePath
AppDomain.CurrentDomain.BaseDirectory
				

If loaded assembly is in same folder then we can also use

Assembly.Location
Assembly.GetExecutingAssembly().CodeBase
				

If we want to get location of loaded assembly, then use above mentioned methods.

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected