Get UNC Path for Mapped Drive VB.net

2019-05-23 02:35发布

I neet to get UNC path from mapped drive. I tried to use WNetGetConnection, but it doesn't work for me. It returns error 487. Does anybody know how to deal with this error or any other way to get the UNC path?

3条回答
姐就是有狂的资本
2楼-- · 2019-05-23 03:07

Works good for me and simpler than an api call like on http://vbnet.mvps.org/index.html?code/network/uncfrommappeddrive.htm The only thing is I had to add a line of code to Dim the variable Line.

Thanks for the help

查看更多
Summer. ? 凉城
3楼-- · 2019-05-23 03:16

Totally go with @Alex K's P/Invoke suggestion, I just wanted to post a hack method of piping through the net use command:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim RemotePath = GetUncSourcePath("v"c)
    If String.IsNullOrEmpty(RemotePath) Then
        Trace.WriteLine("there was an error")
    Else
        Trace.WriteLine(RemotePath)
    End If
    Me.Close()
End Sub
Private Shared Function GetUncSourcePath(ByVal driveLetter As Char) As String
    If String.IsNullOrEmpty(driveLetter) Then Throw New ArgumentNullException("driveLetter")
    If (driveLetter < "a"c OrElse driveLetter > "z") AndAlso (driveLetter < "A"c OrElse driveLetter > "Z") Then Throw New ArgumentOutOfRangeException("driveLetter", "driveLetter must be a letter from A to Z")
    Dim P As New Process()
    With P.StartInfo
        .FileName = "net"
        .Arguments = String.Format("use {0}:", driveLetter)
        .UseShellExecute = False
        .RedirectStandardOutput = True
        .CreateNoWindow = True
    End With
    P.Start()
    Dim T = P.StandardOutput.ReadToEnd()
    P.WaitForExit()
    For Each Line In Split(T, vbNewLine)
        If Line.StartsWith("Remote name") Then Return Line.Replace("Remote name", "").Trim()
    Next
    Return Nothing
End Function
查看更多
萌系小妹纸
4楼-- · 2019-05-23 03:23

You can use the WNetGetUniversalName API.

查看更多
登录 后发表回答