Results 1 to 11 of 11

Thread: [RESOLVED] Get path of mapped network drive

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [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:
    1. For Each drv In IO.DriveInfo.GetDrives()
    2.             If drv.DriveType = IO.DriveType.Network Then
    3.                 MessageBox.Show(drv.RootDirectory.ToString)
    4.             End If
    5. Next

    but this just outputs the drive letters like so:
    Code:
    F:
    G:
    I:
    'etc
    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 'Declared at class level
    2. <DllImport("mpr.dll")> _
    3. Public Shared Function WNetGetConnection(ByVal lpLocalName As String, ByVal lpRemoteName As StringBuilder, ByRef lpnLength As Integer) As Integer
    4. End Function
    5.  
    6. 'Then to use it in a method:
    7. For Each drv In IO.DriveInfo.GetDrives()
    8.             If drv.DriveType = IO.DriveType.Network Then
    9.                 Dim UncPath As New StringBuilder(255)
    10.                 WNetGetConnection(drv.Name.Replace("\", ""), UncPath, UncPath.Capacity)
    11.                 MessageBox.Show(drv.Name & " maps to " & UncPath.ToString)
    12.             End If
    13. Next
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  5. #5
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    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
    Last edited by Cavar; Dec 23rd, 2009 at 10:51 AM.

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get path of mapped network drive

    Quote Originally Posted by kleinma View Post
    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

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    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

  8. #8

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Get path of mapped network drive

    Quote Originally Posted by Cavar View Post
    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    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

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] Get path of mapped network drive

    My guess is any managed code to do this would just wrap that API anyway.

  11. #11

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width