How to create a properly escaped file: // URI in C #?

What would be the right way to create a fully URL-encoded URI file://from a local path, i.e. where all special characters are hidden, such as spaces, etc.

Given the following input

C:\Data\Input Document.txt

I would like to receive

file:///C:/Data/Input%20Document.txt

I used

Uri uri = new Uri(@"C:\Data\Input Document.txt", UriKind.RelativeOrAbsolute); 

However, this leads to an unshielded URI:

file:///C:/Data/Input Document.txt
+5
source share
2 answers

it is already encoded

uri.AbsolutePath should give you "C:/Data/Input%20Document.txt"

+3
source

You tried:

new Uri(str).AbsoluteUri

Not sure if I understand your requirement regarding relative URIs ...

0
source

All Articles