Monday, May 21, 2012 Register  Login

This site uses DNS Made Easy. Use it for reliable and professional DNS services.

RSS Feeds
Categories
  
Blog Archives
  
Blog

Updated code for file path to file URL

After some more testing I found that the URIBuilder class does not work correctly with UNC path. Here's the updated code: 

public static string PathToFileUrl( string pathToConvert )
{
    string fileURL = "";
    bool isUNC;
    UriBuilder fileURI = null;
 
    // This is to capture whether the incoming path is
    // an UNC path or not.
    if ( pathToConvert.StartsWith( @"\\" ) )
    {
        isUNC = true;
    }
    else
    {
        isUNC = false;
    }
 
    try
    {
        fileURI = new UriBuilder( pathToConvert );
    }
    catch( UriFormatException ex )
    {
        // This is for debugging
        string msg = ex.Message;
    }
 
    if ( isUNC )
    {
        if ( fileURI != null ) fileURL = fileURI.ToString();
    }
    else
    {
   if ( fileURI != null ) fileURL = fileURI.ToString().Replace( "file://", "file:///" );
    }
 
    return fileURL;
}

posted @ Wednesday, October 31, 2007 12:33 AM by Hector Sosa, Jr

Posted in: Code

Actions:Tweet This Share on Facebook Share on LinkedIn Emakl Permalink del.icio.us

Previous Page | Next Page

COMMENTS

Isn't the Replace for the file:// required for both case? E.g. UriBuilder("\\foo\bar").ToString() and UrilBuilder("c:\foo\bar").ToString() seem to return "file://foo/bar" and "file://C:/foo/bar" in PowerShell.

posted @ Wednesday, October 31, 2007 6:02 AM by Björn Graf


That's what I thought too. Apparently, UNC paths and drive paths get treated differently by UriBuilder. I noticed this after I got done testing UNC paths and went back to drive paths for testing.

I want to look at UriBuilder with Reflector when I have time.

posted @ Wednesday, October 31, 2007 9:45 AM by Hector Sosa, Jr


Your first example, UriBuilder("\\foo\bar"), would throw an exception, unless "foo" is a host name accessible in your LAN.

posted @ Wednesday, October 31, 2007 9:47 AM by Hector Sosa, Jr


Well, this is the PowerShell command line I used:
$(New-Object System.UriBuilder("\\foo\bar")).ToString()
And there's no accessible foo hostname on my lan. Maybe this is some PowerShell thing...

posted @ Wednesday, October 31, 2007 10:00 AM by Björn Graf


Interesting! That blows up in Visual Studio.

posted @ Wednesday, October 31, 2007 11:08 AM by Hector Sosa, Jr


Name (required)

Email (required)

Website

CAPTCHA image
Enter the code shown above:

Terms Of Use | Privacy Statement | SystemWidgets
Copyright 2002-2012 by SystemWidgets
Google Analytics Alternative