Ask the Expert

How do I get the UNC path from a drive letter in .NET?

How do I get the UNC path from a drive letter in .NET? I tried using the DECLARE wnetgetconnection32 API call, and could not get that working either. Any suggestions?
Keep in mind that WnetGetConnection will only return the UNC path for a mapped drive, not just any drive. The program below will show the UNC name for the mapped Q: drive.
Module Module1

    Public Declare Function WNetGetConnection Lib "mpr.dll" Alias _
             "WNetGetConnectionA" (ByVal lpszLocalName As String, _
             ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As 
Integer

    Sub Main()

        Dim ret As Integer
        Dim out As String = New String(" ", 260)
        Dim len As Integer = 260

        ret = WNetGetConnection("Q:", out, len)

        Console.WriteLine(out)
    End Sub

End Module

This was first published in March 2004