You can use:
string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
Some suggestions in the comments should go through System.Uri.UnescapeDataString (from vvnurmi ) to provide any percentage processing, and use Path.GetFullpath (from TrueWill ) to make sure the path is in the standard Windows form (instead of having oblique traits instead of backslashes). Here is 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 + "]");
Conclusion, if we execute 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]
Reed Copsey May 14 '09 at 16:55 2009-05-14 16:55
source share