[RESOLVED] Get path of mapped network drive
I was expecting this to be easy but it seems that it is not... all I want to do is loop through all drives on a computer and if the drive is a network drive then get the UNC path that this network drive maps to. Simples.
I was expecting to just do this:
vb.net Code:
For Each drv In IO.DriveInfo.GetDrives()
If drv.DriveType = IO.DriveType.Network Then
MessageBox.Show(drv.RootDirectory.ToString)
End If
Next
but this just outputs the drive letters like so:
So does anyone know of any way to get the path that the network drive maps to (e.g \\server\someshare\somedirectory) without using WMI ?
Thanks
Chris
Re: Get path of mapped network drive
Managed to get this done with a Windows API but if anyone has a better solution that uses managed code I would prefer that! :)
Here's my code for anyone that is interested:
vb Code:
'Declared at class level
<DllImport("mpr.dll")> _
Public Shared Function WNetGetConnection(ByVal lpLocalName As String, ByVal lpRemoteName As StringBuilder, ByRef lpnLength As Integer) As Integer
End Function
'Then to use it in a method:
For Each drv In IO.DriveInfo.GetDrives()
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
Re: Get path of mapped network drive
I know I have answered this, but I can't remember if it was the same solution you came up with.. I will see if I can find it.
Re: Get path of mapped network drive
Yeah the solution I had also uses the same API call to WNetGetConnection.
This API call only works in Windows 2000 and up, but I would hope at this point that doesn't matter.
Re: Get path of mapped network drive
You can use the System.Management namespace to get this info, but from my testing it returns the following for a mapped drive, so you would need to clean it up.
file://172.168.0.115/backup
and
file://terastation1/backup
The first was a drive I mapped using the IP and the 2nd I used the server name.
I don't know if it's any better than doing what you did, but here the link I found using Google. It's C#, but easy to convert. You want the "GetUniversalName" method at the top of the page.
Here's a link to the Win32_NetworkConnection Class that lists the various data you can query. I used the RemotePath to get the values above.
Hope it helps,
C
Re: Get path of mapped network drive
Quote:
Originally Posted by
kleinma
Yeah the solution I had also uses the same API call to WNetGetConnection.
This API call only works in Windows 2000 and up, but I would hope at this point that doesn't matter.
Ah well thanks for the confirmation anyway and yeah all of our clients are XP so no worries there :)
Quote:
You can use the System.Management namespace to get this info, but from my testing it returns the following for a mapped drive, so you would need to clean it up.
file://172.168.0.115/backup
and
file://terastation1/backup
The first was a drive I mapped using the IP and the 2nd I used the server name.
I don't know if it's any better than doing what you did, but here the link I found using Google. It's C#, but easy to convert. You want the "GetUniversalName" method at the top of the page.
Here's a link to the Win32_NetworkConnection Class that lists the various data you can query. I used the RemotePath to get the values above.
Hope it helps,
C
Thanks but as I mentioned in the first post, I want to do this without using WMI ;)
Re: [RESOLVED] Get path of mapped network drive
Sometimes my reading suck due to my extreme laziness.
For another non WMI way you could just check the registry under the HKEY_CURRENT_USER\Network key. It will list out all current mapped drives and there's a RemotePath string value that contains the UNC path.
I am not sure if that registry key differs per OS though.
CT
Re: [RESOLVED] Get path of mapped network drive
Quote:
Originally Posted by
Cavar
For another non WMI way you could just check the registry under the HKEY_CURRENT_USER\Network key. It will list out all current mapped drives and there's a RemotePath string value that contains the UNC path.
Interesting, but I have just tested it out and it seems that this is only true for persistent network drives (ie drives that you check the "reconnect at logon" box for) and I want to capture all network drives, especially as some of our logon scripts do not map the drives as persistent. Thanks for the suggestion though, I appreciate it but it looks like I'm going to have to stick to the API for now :thumb:
Re: [RESOLVED] Get path of mapped network drive
Yeah, it appears the ones that you don't reconnect to at login aren't stored in that location, bummer.
C
Re: [RESOLVED] Get path of mapped network drive
My guess is any managed code to do this would just wrap that API anyway.
Re: [RESOLVED] Get path of mapped network drive
Yeah but isnt that what an awful lot of managed code does? I would rather use something that Microsoft have written to wrap their APIs than declare and use the APIs myself any day :)