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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)