获取映射驱动器VB.net的UNC路径

时间:2010-05-04 11:55:24

标签: vb.net mapping

我需要从映射驱动器获取UNC路径。 我尝试使用WNetGetConnection,但它对我不起作用。它返回错误487。 有人知道如何处理这个错误或任何其他方式来获得UNC路径吗?

3 个答案:

答案 0 :(得分:4)

完全符合@Alex K的P / Invoke建议,我只想通过net use命令发布一个管道方法:

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

答案 1 :(得分:1)

您可以使用WNetGetUniversalName API。

答案 2 :(得分:0)

对我有用,比在http://vbnet.mvps.org/index.html?code/network/uncfrommappeddrive.htm上的api调用更简单 唯一的事情是我必须添加一行代码来变暗变量Line。

感谢您的帮助