borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Taksmanager)Next Topic (WIDE AREA NETWORK PROGRAMS) New Topic New Poll Post Reply
AndreaVB Forum : VB General : UNC to a drive letter
Poster Message
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246

icon UNC to a drive letter

Is there a way to convert the UNC portion
(\\server\volume) of a path string to a drive letter?

Let's say I've got a file called G:\Myfile.txt where my G:\ drive is a
networked resource, for example \\server\volume.

How can I return \\server\volume\Myfile.txt in a function, rather
than P:\Myfile.txt?

12-09-2006 at 08:14 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: UNC to a drive letter

Hi Humberto

I found this code, hope it helps. Stick it in a module.

    'Example by Alexey (alexeyka2001@rambler.ru)
    Public Const DRIVE_UNKNOWN = 0
    Public Const DRIVE_ABSENT = 1
    Public Const DRIVE_REMOVABLE = 2
    Public Const DRIVE_FIXED = 3
    Public Const DRIVE_REMOTE = 4
    Public Const DRIVE_CDROM = 5
    Public Const DRIVE_RAMDISK = 6
    ' returns errors for UNC Path
    Public Const ERROR_BAD_DEVICE = 1200&
    Public Const ERROR_CONNECTION_UNAVAIL = 1201&
    Public Const ERROR_EXTENDED_ERROR = 1208&
    Public Const ERROR_MORE_DATA = 234
    Public Const ERROR_NOT_SUPPORTED = 50&
    Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
    Public Const ERROR_NO_NETWORK = 1222&
    Public Const ERROR_NOT_CONNECTED = 2250&
    Public Const NO_ERROR = 0


    Public Declare Function WNetGetConnection Lib "mpr.dll" Alias _
            "WNetGetConnectionA" (ByVal lpszLocalName As String, _
            ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
    Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias _
        "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, _
        ByVal lpBuffer As String) As Long
    Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
        (ByVal nDrive As String) As Long
    Public Function fGetDrives() As String
    'Returns all mapped drives
        Dim lngRet As Long
        Dim strDrives As String * 255
        Dim lngTmp As Long
        lngTmp = Len(strDrives)
        lngRet = GetLogicalDriveStrings(lngTmp, strDrives)
        fGetDrives = Left(strDrives, lngRet)
    End Function
    Public Function fGetUNCPath(strDriveLetter As String) As String
        On Local Error GoTo fGetUNCPath_Err


        Dim Msg As String, lngReturn As Long
        Dim lpszLocalName As String
        Dim lpszRemoteName As String
        Dim cbRemoteName As Long
        lpszLocalName = strDriveLetter
        lpszRemoteName = String$(255, Chr$(32))
        cbRemoteName = Len(lpszRemoteName)
        lngReturn = WNetGetConnection(lpszLocalName, lpszRemoteName, _
                                           cbRemoteName)
        Select Case lngReturn
            Case ERROR_BAD_DEVICE
                Msg = "Error: Bad Device"
            Case ERROR_CONNECTION_UNAVAIL
                Msg = "Error: Connection Un-Available"
            Case ERROR_EXTENDED_ERROR
                Msg = "Error: Extended Error"
            Case ERROR_MORE_DATA
                   Msg = "Error: More Data"
            Case ERROR_NOT_SUPPORTED
                   Msg = "Error: Feature not Supported"
            Case ERROR_NO_NET_OR_BAD_PATH
                   Msg = "Error: No Network Available or Bad Path"


            Case ERROR_NO_NETWORK

                   Msg = "Error: No Network Available"
            Case ERROR_NOT_CONNECTED
                   Msg = "Error: Not Connected"
            Case NO_ERROR
                   ' all is successful...
        End Select
        If Len(Msg) Then
            MsgBox Msg, vbInformation
        Else
            fGetUNCPath = Left$(lpszRemoteName, cbRemoteName)
        End If
fGetUNCPath_End:
        Exit Function
fGetUNCPath_Err:
        MsgBox Err.Description, vbInformation
        Resume fGetUNCPath_End
    End Function


    Public Function fDriveType(strDriveName As String) As String
        Dim lngRet As Long
        Dim strDrive As String
        lngRet = GetDriveType(strDriveName)
        Select Case lngRet
            Case DRIVE_UNKNOWN 'The drive type cannot be determined.
                strDrive = "Unknown Drive Type"
            Case DRIVE_ABSENT 'The root directory does not exist.
                strDrive = "Drive does not exist"
            Case DRIVE_REMOVABLE 'The drive can be removed from the drive.
                strDrive = "Removable Media"
            Case DRIVE_FIXED 'The disk cannot be removed from the drive.
                strDrive = "Fixed Drive"
            Case DRIVE_REMOTE  'The drive is a remote (network) drive.
                strDrive = "Network Drive"
            Case DRIVE_CDROM 'The drive is a CD-ROM drive.
                strDrive = "CD Rom"
            Case DRIVE_RAMDISK 'The drive is a RAM disk.
                strDrive = "Ram Disk"
        End Select
        fDriveType = strDrive
    End Function


    Sub sListAllDrives()
        Dim strAllDrives As String
        Dim strTmp As String
        
        strAllDrives = fGetDrives
        If strAllDrives <> "" Then
            Do
                strTmp = Mid$(strAllDrives, 1, InStr(strAllDrives, vbNullChar) - 1)
                strAllDrives = Mid$(strAllDrives, InStr(strAllDrives, vbNullChar) + 1)
                Select Case fDriveType(strTmp)
                    Case "Removable Media":
                        Debug.Print "Removable drive : " & strTmp
                    Case "CD Rom":
                        Debug.Print " CD Rom drive : " & strTmp
                    Case "Fixed Drive":
                        Debug.Print " Local drive : " & strTmp
                    Case "Network Drive":
                        Debug.Print " Network drive : " & strTmp
                        Debug.Print " UNC Path : " & _
                                    fGetUNCPath(Left$(strTmp, Len(strTmp) - 1))
                End Select
            Loop While strAllDrives <> ""
        End If
    End Sub


    Public Sub Form_Load()
        Debug.Print "All available drives: "
        sListAllDrives
    End Sub


Then to use it, try the following.

Private Sub Command1_Click()

    MsgBox fGetUNCPath(Left(drive1.Drive, 2))
    
End Sub



Steve

[Edited by steve_w on 12-09-2006 at 01:03 PM GMT]

12-09-2006 at 01:02 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : UNC to a drive letter
Previous Topic (Taksmanager)Next Topic (WIDE AREA NETWORK PROGRAMS) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder