How can I convert Assembly.CodeBase into a filesystem path in C#?

Solution 1:

You need to use System.Uri.LocalPath:

string localPath = new Uri("file:///D:/projects/MyApp/MyApp/bin/debug/MyApp.exe").LocalPath;

So if you want the original location of the currently executing assembly:

string localPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;

The LocalPath includes the assembly's file name, e.g.,

D:\projects\MyApp\MyApp\bin\debug\MyApp.exe

If you want the assembly's directory, then use System.IO.Path.GetDirectoryName():

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

which will give you:

D:\projects\MyApp\MyApp\bin\debug

Solution 2:

Assembly.CodeBase looks promising, but it's a UNC path:

Do note that it is something approximating a file uri, not an UNC path.


You solve this by doing string manipulation by hand. Seriously.

Try all other methods you can find on SO with the following directory (verbatim):

C:\Test\Space( )(h#)(p%20){[a&],t@,p%,+}.,\Release

This is a valid, if somewhat unusual, Windows path. (Some people will have either one of these characters in there paths, and you would want you method to work for all of those, right?)

The available code base (we do not want Location, right?) properties are then (on my Win7 with .NET 4):

assembly.CodeBase -> file:///C:/Test/Space( )(h#)(p%20){[a&],t@,p%,+}.,/Release

assembly.EscapedCodeBase -> file:///C:/Test/Space(%20)(h%23)(p%20)%7B%5Ba%26%5D,t@,p%,+%7D.,/Release

You will note:

  • CodeBase is not escaped at all, it's just the regular local path prefixed with file:/// and the backslashes replaced. As such, it does not work to feed this to System.Uri.
  • EscapedCodeBase is not escaped completely (I do not know if this is a bug or if this is a shortcoming of the URI scheme):
    • Note how the space character () translates to %20
    • but the %20 sequence also translates to %20! (percent % is not escaped at all)
    • No one can rebuild the original from this mangled form!

For local files (And that's really all I'd care about for the CodeBasestuff, because if the file ain't local, you probably want to use .Location anyway, the following works for me (note that it isn't the prettiest either:

    public static string GetAssemblyFullPath(Assembly assembly)
    {
        string codeBasePseudoUrl = assembly.CodeBase; // "pseudo" because it is not properly escaped
        if (codeBasePseudoUrl != null) {
            const string filePrefix3 = @"file:///";
            if (codeBasePseudoUrl.StartsWith(filePrefix3)) {
                string sPath = codeBasePseudoUrl.Substring(filePrefix3.Length);
                string bsPath = sPath.Replace('/', '\\');
                Console.WriteLine("bsPath: " + bsPath);
                string fp = Path.GetFullPath(bsPath);
                Console.WriteLine("fp: " + fp);
                return fp;
            }
        }
        System.Diagnostics.Debug.Assert(false, "CodeBase evaluation failed! - Using Location as fallback.");
        return Path.GetFullPath(assembly.Location);
    }

I am sure one can come up with better solutions, probably one could even come up with a solution that does proper URL en-/decoding of the CodeBase property if it's a local path, but given that one can just strip off the file:/// and be done with it, I'd say this solution stands as good enough, if certainly really ugly.

Solution 3:

This should work:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
Assembly asm = Assembly.GetCallingAssembly();
String path = Path.GetDirectoryName(new Uri(asm.EscapedCodeBase).LocalPath);

string strLog4NetConfigPath = System.IO.Path.Combine(path, "log4net.config");

I am using this to be able to log from within the dll libraries using a standalone log4net.config file.

Solution 4:

One more solution, including complex paths:

    public static string GetPath(this Assembly assembly)
    {
        return Path.GetDirectoryName(assembly.GetFileName());
    }

    public static string GetFileName(this Assembly assembly)
    {
        return assembly.CodeBase.GetPathFromUri();
    }

    public static string GetPathFromUri(this string uriString)
    {
        var uri = new Uri(Uri.EscapeUriString(uriString));
        return String.Format("{0}{1}", Uri.UnescapeDataString(uri.PathAndQuery), Uri.UnescapeDataString(uri.Fragment));
    }

and tests:

    [Test]
    public void GetPathFromUriTest()
    {
        Assert.AreEqual(@"C:/Test/Space( )(h#)(p%20){[a&],t@,p%,+}.,/Release", @"file:///C:/Test/Space( )(h#)(p%20){[a&],t@,p%,+}.,/Release".GetPathFromUri());
        Assert.AreEqual(@"C:/Test/Space( )(h#)(p%20){[a&],t@,p%,+}.,/Release",  @"file://C:/Test/Space( )(h#)(p%20){[a&],t@,p%,+}.,/Release".GetPathFromUri());
    }

    [Test]
    public void AssemblyPathTest()
    {
        var asm = Assembly.GetExecutingAssembly();

        var path = asm.GetPath();
        var file = asm.GetFileName();

        Assert.IsNotEmpty(path);
        Assert.IsNotEmpty(file);

        Assert.That(File     .Exists(file));
        Assert.That(Directory.Exists(path));
    }