Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove file:// prefix c#

Tags:

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

like image 347
Evyatar Avatar asked Dec 07 '22 20:12

Evyatar


2 Answers

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; 

UPDATE:

More robust implementation eliminating string.Concat to handle other cases including fragments in the codebase url.

var codebase = new Uri(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

var file = new Uri(codebase, "bla.xml").AbsolutePath;
like image 129
Wagner DosAnjos Avatar answered Dec 23 '22 21:12

Wagner DosAnjos


Use the Assembly.Location property instead, which returns a normal filepath.

Also, use Path.Join().

like image 36
SLaks Avatar answered Dec 23 '22 22:12

SLaks