I recently had a need in one of my programs to loop through all network drives on a computer and get the drive letter and the UNC path that the network drive mapped to. I thought this would be easy with the System.IO.DriveInfo.GetDrives method but unfortunately it was not as the DriveInfo class offers no way to retrieve the UNC path that the network drive maps to. After a bit of research I could find no better way than using a Windows API (I’m not a fan of WMI!). Here is some example VB.NET code that I have written that will do this:
<DllImport("mpr.dll")> _
Public Shared Function WNetGetConnection(ByVal lpLocalName As String, ByVal lpRemoteName As StringBuilder, ByRef lpnLength As Integer) As Integer
End Function
'Example method:
Private Sub SomeMethod()
'Loop through all drives
For Each drv In IO.DriveInfo.GetDrives()
'If this is a network drive then call the API
If drv.DriveType = IO.DriveType.Network Then
Dim UncPath As New StringBuilder(255)
WNetGetConnection(drv.Name.Replace("\", ""), UncPath, UncPath.Capacity)
MessageBox.Show(drv.Name & " maps to " & UncPath.ToString)
End If
Next
End Sub
I also posted this on vbforums: http://www.vbforums.com/showthread.php?t=596880
I followed you over here from the VBForums and noticed that you mentioned nicely formated code back at the forum. If you use Visual Studio you may want to check out CopySourceAsHtml
http://copysourceashtml.codeplex.com/
It is VS add in that will add nicely formated code blocks to Word Press or any other blog software. Also if you use MS Live Writer to write your blog posts you can use the Paste From Visual Studio plug in (Which I like better) that does the same thing.
Thanks for that, I will certainly check it out! 🙂
I had a look at both options and I ended up using the Windows Live Writer add-in that you suggested and it works great so thanks a lot for telling me about that 🙂