2010-05-04 121 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

相關問題