tags:

views:

1287

answers:

2

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

Given the following input

C:\Data\Input Document.txt

I would like to get

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

I've been using

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

However, this results in an unescaped URI:

file:///C:/Data/Input Document.txt
+1  A: 

it is already encoded

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

Rony
Thanks, I missed that. However, it is only working for absolute URIs as this property is not set for relative URIs.
0xA3
A: 

Have you tried:

new Uri(str).AbsoluteUri

Not sure I understand your requirement for relative URIs...

Lachlan