2010-05-04 85 views
2

我需要从映射驱动器获取UNC路径。 我试图使用WNetGetConnection,但它不适合我。它返回错误487. 有没有人知道如何处理这个错误或任何其他方式来获得UNC路径?获取映射驱动器的UNC路径VB.net

回答

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 
+0

不为我工作...一直没有返回:( – Nitesh 2011-09-29 05:13:25

+0

@ rbsoft.sol,下降到命令行运行'净使用v:','替换五:用'你的映射驱动器,你看到一行说'Remote name'? – 2011-09-29 13:06:37

+0

@ Chris..No,我得到“网络连接无法找到”..我正在使用网络c: – Nitesh 2011-09-30 04:14:56

相关问题