Converting File URIs to Paths

I spent most of this morning looking for a replacement to Application.ExecutablePath.  The reason for this was because certain unit tests that depended on this code failed when a used any test runner other than the NUnit GUI.  When using test runners like ReSharper and TestDriven.NET, Application.ExecutablePath returned the executable of the test runner, instead of the DLL being tested.

Assembly.GetExecutingAssembly().CodeBase returned a file URI with the DLL I wanted, but subsequent code which used the value threw an exception because it didn’t accept file URIs.  This made it necessary to convert the file URI into a regular path.  I haven’t found a .NET Framework method that does this yet, but the following code seems to do the trick:


private static string ConvertUriToPath(string fileName)
{
fileName = fileName.Replace("file:///", "");
fileName = fileName.Replace("/", "\");
return fileName;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.