From file to file:// in C#

Convert file path to file URL

A very short note: I was trying to find an analogue to Java’s File.toURI().toURL() method in .NET, looking through System.IO et al, and not finding it on the lazyweb.

To my surprise, all you need use is UriBuilder:

new UriBuilder(@"c:\path\to\file.txt").ToString();

This will output “file://c:/path/to/file.txt“. Simple, but far too much time wasted finding it…

No Comments

Post a Comment

Your email is never shared. Required fields are marked *