Click here to Skip to main content
Click here to Skip to main content
Go to top

Convert a file path to a UNC Path

, 17 Feb 2011
Rate this:
Please Sign up or sign in to vote.
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:
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)

Share

About the Author

TomQuinn
Software Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionWMI not WSH - Imports System.Management PinmemberE.Wilkins17-Feb-14 2:18 
Questionin WPF Pinmemberroland_12327-Aug-13 20:48 
AnswerRe: in WPF PinmemberTomQuinn27-Aug-13 22:04 
GeneralRe: in WPF Pinmemberroland_1235-Nov-13 23:54 
General"You will need a reference to the Windows Script Host Object... PinmemberRichard Deeming4-Mar-11 10:38 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 17 Feb 2011
Article Copyright 2011 by TomQuinn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid