45

How do I get the path of the current assembly? I need to get data from some paths relative to the location of hte current assembly (.dll).

I thought someone told me to use the reflection namespace but I can't find anything in there.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
DenaliHardtail
  • 27,362
  • 56
  • 154
  • 233

4 Answers4

82

You can use:

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

Some suggestions in the comments are to pass that through System.Uri.UnescapeDataString (from vvnurmi) to ensure that any percent-encoding is handled, and to use Path.GetFullpath (from TrueWill) to ensure that the path is in standard Windows form (rather than having slashes instead of backslashes). Here's an example of what you get at each stage:

string s = Assembly.GetExecutingAssembly().CodeBase;
Console.WriteLine("CodeBase: [" + s + "]");
s = (new Uri(s)).AbsolutePath;
Console.WriteLine("AbsolutePath: [" + s + "]");
s = Uri.UnescapeDataString(s);
Console.WriteLine("Unescaped: [" + s + "]");
s = Path.GetFullPath(s);
Console.WriteLine("FullPath: [" + s + "]");

Output if we're running C:\Temp\Temp App\bin\Debug\TempApp.EXE:

CodeBase: [file:///C:/Temp/Temp App/bin/Debug/TempApp.EXE]
AbsolutePath: [C:/Temp/Temp%20App/bin/Debug/TempApp.EXE]
Unescaped: [C:/Temp/Temp App/bin/Debug/TempApp.EXE]
FullPath: [C:\Temp\Temp App\bin\Debug\TempApp.EXE]
Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 8
    I'd recommend calling Path.GetFullPath to turn the forward slashes into backslashes. You can get weird behavior from Assembly.LoadFile (such as the types not equating) when using forward slashes. – TrueWill Apr 01 '11 at 19:23
  • 3
    What is the advantage/purpose of using this instead of `.Location`? (I suspect there must be a reason, but it eludes me.) –  Apr 03 '11 at 18:34
  • 4
    @pst: Depending on the environment, Location can be incorrect, since shadow copying can "move" the assembly prior to execution. – Reed Copsey Apr 03 '11 at 20:33
  • 1
    @Reed: According [to MSDN](http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx) `Location` should work in the case of shadow-copying, quote: "The location of the loaded file that contains the manifest. If the loaded file was shadow-copied, the location is that of the file after being shadow-copied.". Wouldn't that be correct behavior? Have you encountered situations where `Location` failed unexpectedly? – Marijn Dec 06 '11 at 08:59
  • 10
    @Marijn That's the problem - it's the location AFTER shadow copying. Typically, you want the location of the actual file prior to load, which the above gives you... – Reed Copsey Dec 06 '11 at 16:17
  • +1 - this is exactly what I needed for some dynamic loading of entity framework config classes. I'd give +10 if I could :) – Antony Scott Jan 24 '12 at 20:30
  • This is very useful when running unit test, as test framework might shadow copy your assembly to a temporary folder. – Baiyan Huang Mar 16 '12 at 07:17
  • 1
    When I do this I get spaces turned into '%' – C.J. Jun 05 '12 at 19:38
  • 8
    Call `System.Uri.UnescapeDataString` to turn a percent-encoded file path back into a normal file path. The Uri constructor will encode e.g. spaces as "%20" which will be understood literally by .NET methods that expect conventional file paths and not URIs. – vvnurmi Apr 05 '13 at 07:43
46

System.Reflection.Assembly.GetExecutingAssembly().Location

knocte
  • 16,941
  • 11
  • 79
  • 125
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
7

Just to make it clear:

Assembly.GetExecutingAssembly().Location
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
3

I prefer

new System.Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;

EscapedCodeBase covers the scenario where your local path might have an invalid URI char in it (see https://stackoverflow.com/a/21056435/852208)

LocalPath includes the full path for both local paths and unc paths, where AbsolutePath trims off "\\server"

Community
  • 1
  • 1
b_levitt
  • 7,059
  • 2
  • 41
  • 56