Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Convert a file path to a UNC Path

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Feb 2011CPOL 52.4K   11   5
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)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWMI not WSH - Imports System.Management Pin
E.Wilkins17-Feb-14 2:18
E.Wilkins17-Feb-14 2:18 
Hi - nice little bit of code, thank you. It was probably a typo, I think you meant Windows Management Instrumentation instead of WSH. You need a reference to System.Management and add Imports System.Management to your module or class.

Cheers
Eric
Thumbs Up | :thumbsup:
(Update - sorry; should have looked through all the comments - I see a reference to this already (pun intended))
Questionin WPF Pin
roland_12327-Aug-13 20:48
professionalroland_12327-Aug-13 20:48 
AnswerRe: in WPF Pin
TomQuinn27-Aug-13 22:04
TomQuinn27-Aug-13 22:04 
GeneralRe: in WPF Pin
roland_1235-Nov-13 23:54
professionalroland_1235-Nov-13 23:54 
General"You will need a reference to the Windows Script Host Object... Pin
Richard Deeming4-Mar-11 10:38
mveRichard Deeming4-Mar-11 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.