Get Currently Executing Assembly’s Directory

Sometimes it becomes necessary to find out the directory where the current executing assembly is running. Among several ways to do the task  , the following code–snippet can be employed  to return the absolute location of the loaded file that contains the manifest:

using System.Reflection;
string currentAssemblyDirectoryName = Path.GetDirectoryName(
Assembly.GetExecutingAssembly()
.Location
);
view raw gistfile1.cs hosted with ❤ by GitHub

Beware that Assembly.Location property returns the location of the assembly file after it is shadow-copied. In addition , if it is dynamically loaded using Load method, it returns empty string .

As stated in the comments below, if you are interested in retrieving the location of the assembly before it is shadow-copied or loaded,  use Assembly.CodeBase property instead. Please note that, it is the location specified originally while stating the AssemblyName.

Advertisement

5 thoughts on “Get Currently Executing Assembly’s Directory”

  1. Hi,

    Assembly.GetExecutingAssembly().Location does not return the current path of the assembly.

    Please use

    Uri assemblyUri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
    string path = Path.GetDirectoryName(assemblyUri.LocalPath);

    Thanks

  2. Thanks Muhammad,

    Indeed you are right in the case when you would like to get the location before the assembly is shadow-copied.

    Regards, Adil

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: