How to determine if a given UNC network share has already been mapped to a drive letter

I have an app that needs to write a script that will need to access files on a network drive -- call it \MyServer\Share. The program running the script cannot deal with UNC's. On some systems this (\MyServer\Share) has already been mapped on others not. My current logic finds an empty drive and tries to map to that drive, but fails with error 1219 (Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed).

How can I check to see if the share (\MyServer\Share) is already mapped?


Ok thanks in part to Fadi's comment, which sent me down a slightly different google path I found my way to this answer . This answer was designed to find the UNC of a file, so I slightly modified it.

Here is my code:

Public Shared Function GetDriveUNC(ByVal sDrive As String) As String

If sDrive.IndexOf(":") Then
    Dim searcher As New ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + sDrive.Substring(0, 2) + "'")
    For Each managementObject As ManagementObject In searcher.[Get]()
        Dim sRemoteName As String = TryCast(managementObject("RemoteName"), String)
        Return (New Uri(sRemoteName)).ToString()
    Next
    Return sDrive
    Else
        Return sDrive
    End If

End Function