2009-04-07 114 views
2

我想創建一個函數將映射的驅動器路徑轉換爲UNC路徑。我做了一些Google搜索,發現severaldifferentpages就此事。我對第一個感興趣,但似乎無法得到合作的代碼。由於原來的職位是在C#中,這是我的VB.NET轉換:VB.NET管理對象幫助

Public Shared Function GetUniversalName(ByVal sFilePath As String) As String 
    If sFilePath = String.Empty OrElse sFilePath.IndexOf(":") Then 
     Return sFilePath 
    End If 
    If sFilePath.StartsWith("\") Then 
     Return (New Uri(sFilePath)).ToString() 
    End If 

    Dim searcher As New ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + sFilePath.Substring(0, 2) + "'") 
    For Each managementObject As ManagementObject In searcher.[Get]() 
     Dim sRemoteName As String = TryCast(managementObject("RemoteName"), String) 
     sRemoteName += sFilePath.Substring(2) 
     Return (New Uri(sRemoteName)).ToString() 
    Next 
    Return sFilePath 
End Function 

我有進口System.Management,但VS2008說沒有定義ManagementObjectSearcherManagementObject。任何人都可以把我推向正確的方向嗎?

回答

4

您還需要在您的參考中添加System.Management。