Getting the path of the current assembly
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.
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]
System.Reflection.
Assembly
.GetExecutingAssembly()
.Location
Just to make it clear:
Assembly.GetExecutingAssembly().Location