Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Convert a file path to a UNC Path

0.00/5 (No votes)
17 Feb 2011CPOL 53.6K  
A simple function used to determine if a file path refers to a network drive and if it does convert it to use the UNC path
When using an OpenFileDialog in .NET, it is often useful to be able to ascertain if the file selected resides on a network drive. If this is the case and you will be storing the file path for others to use (in a database for example), then you are relying on all users having the same drives mapped to the same letters.

The following code will convert a file path to a UNC path if the file is on a network drive.
'Function will take a file path and if it points to a
'mapped drive then it will return the UNC path.

Public Shared Function convertFilePath(ByVal strPath As String) As String
    'First check if file path is already a UNC path.
    If strPath.Length > 2 Then
        If strPath.Substring(0, 2) = "\\" Then Return strPath 'If it is return it.
    Else
        'Path is too short so return strPath to stop app from crashing
        Return strPath
    End If

    'Path is not already a UNC path so use a
    'Windows Script Host Object Model to search all
    'network drives and record their letters and
    'paths in a hashtable.
    Dim htCurrentMappings As New Hashtable
    Dim objQuery As New WqlObjectQuery("select DriveType,DeviceID,ProviderName from Win32_LogicalDisk where DriveType=4")
    Dim objScope As New ManagementScope("\\.\root\CIMV2")
    objScope.Options.Impersonation = ImpersonationLevel.Impersonate
    objScope.Options.EnablePrivileges = True
    Dim objSearcher As New ManagementObjectSearcher(objScope, objQuery)
    For Each objManagementObject As ManagementObject In objSearcher.Get
        htCurrentMappings.Add(objManagementObject("DeviceID").ToString, objManagementObject("ProviderName").ToString)
    Next
    'Mapped drive letters and paths are now stored
    'in htCurrentMappings.

    'Get drive letter from strPath
    Dim strDriveLetter As String = strPath.Substring(0, 2)

    'Check if drive letter is a network drive
    If htCurrentMappings.Contains(strDriveLetter) Then
        'If it is return path with drive letter replaced by UNC path
        Return strPath.Replace(strDriveLetter, htCurrentMappings(strDriveLetter).ToString)
    Else
        'Else just return path as it is most likely local
        Return strPath
    End If
End Function


To use the function with an OpenFileDialog, use the following code:
VB
Dim strCorrectedPath as String
Dim objFileBrowser As New OpenFileDialog
If objFileBrowser.ShowDialog() = Windows.Forms.DialogResult.OK Then
    strCorrectedPath = 
convertFilePath(objFileBrowser.FileName)
End If


You will need a reference to the Windows Script Host Object Model COM component for this code to work.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)