Visual Basic Internet Routines
UrlCreateFromPath: Proper URL Path Conversion from a DOS Path
Posted:   Monday July 09, 2001
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   See prerequisites below
Author:   VBnet - Randy Birch


UrlUnescape: Encoding and Decoding URL Escape Characters 
UrlCanonicalize: Proper URL Path Encoding and Decoding 
UrlGetPart: Determine the Constituent Parts of a URL
Shlwapi.dll version 5.00 or greater, Windows XP, 2000, Windows NT4 with IE 5 or later, Windows 98, or Windows 95 with IE 5 or later. 

UrlCreateFromPath, one of the Shell Lightweight Utility APIs, takes a a DOS path and converts it to a canonicalized URL.  The function only works on paths that do not have an http:// prefix, returning strings suitable for use in a web page to access files on a specific machine. The DOS file path can reference a remote machine either by host name or by IP address. Where the file required conversion of spaces to escape characters, UrlCreateFromPath handles this chore automatically.
 BAS Module Code

 Form Code
To a form add a command button (Command1) and six text boxes (Text1 through Text6) along with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const MAX_PATH                   As Long = 260
Private Const ERROR_SUCCESS              As Long = 0

Private Declare Function UrlCreateFromPath Lib "shlwapi" _
   Alias "UrlCreateFromPathA" _
  (ByVal pszPath As String, _
   ByVal pszUrl As String, _
   pcchUrl As Long, _
   ByVal dwReserved As Long) As Long

Private Sub Form_Load()

   Text1.Text = "http://vbnet code lib/net code/../ip address.htm"
   Text1.Text = "c:\my documents\vbnet articles\random access.doc"
   Text2.Text = ""
   Text3.Text = "c:\documents\vbnet\randomaccess.doc"
   Text4.Text = ""
   Text5.Text = "\\\vbnet\randomaccess.htm"
   Text6.Text = ""
   Command1.Caption = "Create From Path"

End Sub

Private Sub Command1_Click()

   Dim sPath As String
   Dim sUrl As String

  'use the original string in Text1 for
  'demo, and show results in Text2
   sPath = Text1.Text
   sUrl = CreateUrlFromPath(sPath)
   Text2.Text = sUrl
  'use the original string in Text3 for
  'demo, and show results in Text4
   sPath = Text3.Text
   sUrl = CreateUrlFromPath(sPath)
   Text4.Text = sUrl
  'use the original string in Text5 for
  'demo, and show results in Text6
   sPath = Text5.Text
   sUrl = CreateUrlFromPath(sPath)
   Text6.Text = sUrl

End Sub

Private Function CreateUrlFromPath(ByVal sPath As String) As String

   Dim sUrl As String
   Dim dwSize As Long
   If Len(sPath) > 0 Then
      sUrl = Space$(MAX_PATH)
      dwSize = Len(sUrl)
      If UrlCreateFromPath(sPath, _
                           sUrl, _
                           dwSize, _
                           0&) = ERROR_SUCCESS Then
         CreateUrlFromPath = Left$(sUrl, dwSize)
      End If  'If UrlCreateFromPath
   End If 'If Len(sUrl) > 0

End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter