Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

uri

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
like image 803
Dirk Vollmar Avatar asked Jun 23 '09 17:06

Dirk Vollmar


1 Answers

it is already encoded

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

like image 144
Rony Avatar answered Sep 28 '22 12:09

Rony