Click here to Skip to main content
12,298,463 members (46,512 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked

Convert a file path to a UNC Path

, 17 Feb 2011 CPOL
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.
        '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)
    '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 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 = 
End If

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


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


About the Author

Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWMI not WSH - Imports System.Management Pin
E.Wilkins17-Feb-14 2:18
memberE.Wilkins17-Feb-14 2:18 
Questionin WPF Pin
roland_12327-Aug-13 20:48
memberroland_12327-Aug-13 20:48 
AnswerRe: in WPF Pin
TomQuinn27-Aug-13 22:04
memberTomQuinn27-Aug-13 22:04 
GeneralRe: in WPF Pin
roland_1235-Nov-13 23:54
memberroland_1235-Nov-13 23:54 
General"You will need a reference to the Windows Script Host Object... Pin
Richard Deeming4-Mar-11 10:38
memberRichard Deeming4-Mar-11 10:38 
"You will need a reference to the Windows Script Host Object Model COM component for this code to work."

Why? You're not using it anywhere, and it works without the reference.

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.

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