tags:

views:

191

answers:

3

I have an LPTSTR for a file path, i.e. C:\Program Files\Ahoy. I would like to convert it to a file:// URL that I can pass to ShellExecute in order to start the system's default browser pointing at the file. I don't want to give the path to ShellExecute directly since file associations may result in it being opened by something other than a web browser. The path is arbitrary, and may contain characters that need to be escaped.

Is there an existing library function, along the lines of Python's urllib.pathname2url, that does this translation? This can be done via the Uri class in .NET, but I haven't found anything for plain win32.

A: 

Surely it just comes down to replacing the "\" with "/" and adding file:// on the front??

Goz
If you're not thinking about edge-cases you're not doing it right, and there are some for this question, though not too many.
Roger Pate
I'm obviously NOT thinking of edge cases .. would you care to enlighten me? :)
Goz
Character escaping, for one. The set of characters allowed in a path is not the same as the set allowed for URLs. For example, a space would need to be translated into either "+" or "%20".
Tim Sylvester
Also UNC paths which have their own convention: `file:///C|/path/filename`
the_mandrill
yes, and other problems listed at http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx
Sheng Jiang 蒋晟
Interesting :) Cheers guys. I've always found no problems with the spaces thing (for example), i've always found it got auto appended. Fair points though :)
Goz
+5  A: 

There's the UrlCreateFromPath API:

http://msdn.microsoft.com/en-us/library/bb773773%28VS.85%29.aspx

Tim Sylvester
+3  A: 

There's an entire path handling library within Win32. It's called Shell Path Handling Functions.

Assaf Lavie