UrlCreateFromPath (shlwapi)
Last changed: -65.219.124.65

.
Summary

C# Signature:

    [DllImport("shlwapi.dll", CharSet=CharSet.Auto)]
    static extern int UrlCreateFromPath(
    [In]     string path,
    [Out]    StringBuilder url,
    [In,Out] ref uint urlLength,
    [In]     uint reserved
    );

VB Signature:

    Declare Unicode Function UrlCreateFromPath Lib "shlwapi.dll" Alias "UrlCreateFromPathW" _
    (ByVal path As String, _
     ByVal url As System.Text.StringBuilder, _
     ByRef urlLength As System.UInt32, _
     ByVal reserved As Integer _
    ) As Integer

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

The return string size will be INTERNET_MAX_URL_LENGTH (defined in wininet.h) or smaller.

Returns an HRESULT; S_FALSE indicates that the path specified was already in Url format and was copied unchanged to url.

Tips & Tricks:

Please add some!

Sample Code (C# console):

    [STAThread]
    static void Main(string[] args)
    {
      Console.Write(@"Enter filename: ");
      string filename = Console.ReadLine();
      Console.WriteLine(UrlFromPath(filename));

      Console.Read();
    }

    private static string UrlFromPath(string filepath)
    {
      uint maxLen=2048+32+3;//see INTERNET_MAX_URL_LENGTH
      StringBuilder url = new StringBuilder((int)maxLen);
      UrlCreateFromPath(filepath,url,ref maxLen,0);
      return url.ToString();
    }

Sample Code (VB winform):

    Const INTERNET_MAX_URL_LENGTH As Integer = 2048 + 32 + 3

    Private Sub urlFromPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles urlFromPathButton.Click
      Dim sz As System.UInt32
      Dim sb As System.Text.StringBuilder
      Dim rc As Integer

      sz = Convert.ToUInt32(INTERNET_MAX_URL_LENGTH)
      sb = New System.Text.StringBuilder(INTERNET_MAX_URL_LENGTH)

      rc = UrlCreateFromPath(pathTextbox.Text, sb, sz, 0)

      urlTextbox.Text = sb.ToString()
    End Sub

Documentation