Get UNC path from network drive

December 23, 2009 — 3 Comments

I recently had a need in one of my programs to loop through all network drives on a computer and get the drive letter and the UNC path that the network drive mapped to. I thought this would be easy with the System.IO.DriveInfo.GetDrives method but unfortunately it was not as the DriveInfo class offers no way to retrieve the UNC path that the network drive maps to. After a bit of research I could find no better way than using a Windows API (I’m not a fan of WMI!). Here is some example VB.NET code that I have written that will do this:

'Declaring the API
<DllImport("mpr.dll")> _
Public Shared Function WNetGetConnection(ByVal lpLocalName As String, ByVal lpRemoteName As StringBuilder, ByRef lpnLength As Integer) As Integer
End Function

'Example method:
Private Sub SomeMethod()
    'Loop through all drives
    For Each drv In IO.DriveInfo.GetDrives()
        'If this is a network drive then call the API
        If drv.DriveType = IO.DriveType.Network Then
            Dim UncPath As New StringBuilder(255)
            WNetGetConnection(drv.Name.Replace("\", ""), UncPath, UncPath.Capacity)
            MessageBox.Show(drv.Name & " maps to " & UncPath.ToString)
        End If
End Sub

I also posted this on vbforums:

3 responses to Get UNC path from network drive


    I followed you over here from the VBForums and noticed that you mentioned nicely formated code back at the forum. If you use Visual Studio you may want to check out CopySourceAsHtml

    It is VS add in that will add nicely formated code blocks to Word Press or any other blog software. Also if you use MS Live Writer to write your blog posts you can use the Paste From Visual Studio plug in (Which I like better) that does the same thing.


    I had a look at both options and I ended up using the Windows Live Writer add-in that you suggested and it works great so thanks a lot for telling me about that 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s