13

I need to convert UNC paths to file:/// URLs. For example:

\\fileserver\share\dir\some file.ext --> file://///fileserver/share/dir/some%20file.ext

Is there a built-in function for this?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • That doesn't look like the right output. `file://///fileserver/share/file.txt` means a file on localhost whose name is `//fileserver/share/file.txt`. – Lucian Wischik Jun 12 '17 at 22:25

1 Answers1

13

Yes, use the Uri class in the System namespace:

Uri uri = new Uri(@"\\fileserver\share\dir\some file.ext");
string url = uri.AbsoluteUri;
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
Jon Benedicto
  • 10,492
  • 3
  • 28
  • 30
  • 2
    This gives "file://fileserver/share/dir/some%20file.ext" and not "file://///fileserver/share/dir/some%20file.ext" which op explicitly requested. What the "correct" way of encoding UNC paths as urls is can be a lenghty discussion, see e.g. https://bugzilla.mozilla.org/show_bug.cgi?id=66194 - but nevertheless this fails at answering op's question. – poizan42 Mar 01 '16 at 21:36