How to get the Application Directory in C#?

If you have been developing application using C#, I am sure you would have come across a situation where you have to find the Application Directory Path.

There are many ways to get the Application Directory in your C# Application. I have listed below some of the ways you can accomplish this.  

 

Method 1:

Here is an example of how you can get the Application Directory Name where you have your EXE.

This takes the Path of the EXE of your application and passes it to the GetDirectoryName method to get the name of the directory.

 

using System.IO;

using System.Windows.Forms;

 

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

 

Method 2:

Next to get the Directory name of the Executing Assembly you can use the following code:

System.IO.Path.GetDirectoryName
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase

Here is an example of getting the Directory Name of the Executing Assembly.

         String strAppDir = Path.GetDirectoryName(

            Assembly.GetExecutingAssembly().GetName().CodeBase);

        String strFullPathToMyFile = Path.Combine(strAppDir, "fileName.txt");

 

        MessageBox.Show(String.Format("Path to the application is: '{0}'." +

            "Full path to the file in the application folder is: '{1}'",

            strAppDir, strFullPathToMyFile));

 

Method 3

You can use this code in your C# Windows Application to get the Application Directory Name

string msg = Application.StartupPath;

 

Method 4

AppDomain.CurrentDomain.BaseDirectory 

 

Method 5

System.IO.Directory.GetCurrentDirectory() 

 

Method 6

Environment.CurrentDirectory 

 

Hope this was helpful.