Search code examples
c#.netpathuri

Convert file path to a file URI?


Does the .NET Framework have any methods for converting a path (e.g. "C:\whatever.txt") into a file URI (e.g. "file:///C:/whatever.txt")?

The System.Uri class has the reverse (from a file URI to absolute path), but nothing as far as I can find for converting to a file URI.

Also, this is not an ASP.NET application.


Solution

  • The System.Uri constructor has the ability to parse full file paths and turn them into URI style paths. So you can just do the following:

    var uri = new System.Uri("c:\\foo");
    var converted = uri.AbsoluteUri;