Evyatar Evyatar - 17 days ago 5
C# Question

Remove file:// prefix c#

I have the follow:

string file = string.Concat(
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
),
"bla.xml");


Now file is:

file:\C:\test\Debugbla.xml


How can i remove the "file:\" prefix?

Thanks

Answer

Uri class is the way to manipulate Uri. While string.Replace is an option it is better to use tools explicitly designed for particular case which also take care of handling escaping rules where necessary.

string file = string.Concat(System.IO.Path.GetDirectoryName(
     System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bla.xml");

file = new Uri(file).AbsolutePath; 
Comments