locked
Network Drive Detect RRS feed

  • Question

  • I have a favor to ask. The following code is supposed to detect all drives currently active. Since I have no access to a network drive, would someone connected to a network drive be kind enough to insert this code into a Public Function test module for me and make sure it does detect the existence of a network drive? Just enter GetNetwork in the immediate window. If your network drive is Z for example, it should give you Z: as a DeviceID. Thank you so much. Just let me know if it worked or not.

    Public Function GetNetwork()
    Dim objWMI As Object
    Dim colDisks As Object
    Dim objDisk As Object
    Dim strDriveType As String
    Set objWMI = GetWMIService
    Set colDisks = objWMI.ExecQuery("Select * from Win32_LogicalDisk")
    For Each objDisk In colDisks
      With objDisk
        Select Case .DriveType
          Case 0
            strDriveType = "Unknown"
          Case 1
            strDriveType = "No Root Directory"
          Case 2
            strDriveType = "Removable Disk"
          Case 3
            strDriveType = "Local Disk"
          Case 4
            strDriveType = "Network Drive"
          Case 5
            strDriveType = "Compact Disc"
          Case 6
            strDriveType = "RAM Disk"
        End Select
        If (strDriveType = "Local Disk") Or (strDriveType = "Network Drive") Then
            Debug.Print "DeviceID: " & .DeviceID
            Debug.Print "Drive Type: " & strDriveType
            Debug.Print "File System: " & .FileSystem
        End If
      End With
    Next objDisk
    End Function

    Public Function GetWMIService() As Object
    Dim strComputer As String
       strComputer = "."
       Set GetWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    End Function

    Wednesday, February 5, 2014 10:59 PM

Answers

  • It worked for me as a VB.Net 4.5 Console Application running under Windows 7.  I only had to tweak a thing or two.

    Module Module1
    
        Sub Main()
            GetNetwork()
        End Sub
    
        Public Sub GetNetwork()
            Dim objWMI As Object
            Dim colDisks As Object
            Dim objDisk As Object
            Dim strDriveType As String
            objWMI = GetWMIService()
            colDisks = objWMI.ExecQuery("Select * from Win32_LogicalDisk")
            For Each objDisk In colDisks
                With objDisk
                    Select Case .DriveType
                        Case 0
                            strDriveType = "Unknown"
                        Case 1
                            strDriveType = "No Root Directory"
                        Case 2
                            strDriveType = "Removable Disk"
                        Case 3
                            strDriveType = "Local Disk"
                        Case 4
                            strDriveType = "Network Drive"
                        Case 5
                            strDriveType = "Compact Disc"
                        Case 6
                            strDriveType = "RAM Disk"
                        Case Else
                            strDriveType = ""
                    End Select
                    If (strDriveType = "Local Disk") Or (strDriveType = "Network Drive") Then
                        Debug.Print("DeviceID: " & .DeviceID)
                        Debug.Print("Drive Type: " & strDriveType)
                        Debug.Print("File System: " & .FileSystem)
                    End If
                End With
            Next objDisk
        End Sub
    
        Public Function GetWMIService() As Object
            Dim strComputer As String
            strComputer = "."
            GetWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        End Function
    End Module
    Output:

    DeviceID: C:
    Drive Type: Local Disk
    File System: NTFS
    DeviceID: G:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: L:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: M:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: O:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: Q:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: S:
    Drive Type: Network Drive
    File System: NTFS



    • Edited by Peter Schlosser Wednesday, February 5, 2014 11:38 PM
    • Marked as answer by Marvin_Guo Thursday, February 13, 2014 1:10 PM
    Wednesday, February 5, 2014 11:24 PM

All replies

  • It worked for me as a VB.Net 4.5 Console Application running under Windows 7.  I only had to tweak a thing or two.

    Module Module1
    
        Sub Main()
            GetNetwork()
        End Sub
    
        Public Sub GetNetwork()
            Dim objWMI As Object
            Dim colDisks As Object
            Dim objDisk As Object
            Dim strDriveType As String
            objWMI = GetWMIService()
            colDisks = objWMI.ExecQuery("Select * from Win32_LogicalDisk")
            For Each objDisk In colDisks
                With objDisk
                    Select Case .DriveType
                        Case 0
                            strDriveType = "Unknown"
                        Case 1
                            strDriveType = "No Root Directory"
                        Case 2
                            strDriveType = "Removable Disk"
                        Case 3
                            strDriveType = "Local Disk"
                        Case 4
                            strDriveType = "Network Drive"
                        Case 5
                            strDriveType = "Compact Disc"
                        Case 6
                            strDriveType = "RAM Disk"
                        Case Else
                            strDriveType = ""
                    End Select
                    If (strDriveType = "Local Disk") Or (strDriveType = "Network Drive") Then
                        Debug.Print("DeviceID: " & .DeviceID)
                        Debug.Print("Drive Type: " & strDriveType)
                        Debug.Print("File System: " & .FileSystem)
                    End If
                End With
            Next objDisk
        End Sub
    
        Public Function GetWMIService() As Object
            Dim strComputer As String
            strComputer = "."
            GetWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        End Function
    End Module
    Output:

    DeviceID: C:
    Drive Type: Local Disk
    File System: NTFS
    DeviceID: G:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: L:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: M:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: O:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: Q:
    Drive Type: Network Drive
    File System: NTFS
    DeviceID: S:
    Drive Type: Network Drive
    File System: NTFS



    • Edited by Peter Schlosser Wednesday, February 5, 2014 11:38 PM
    • Marked as answer by Marvin_Guo Thursday, February 13, 2014 1:10 PM
    Wednesday, February 5, 2014 11:24 PM
  • OK Peter, thank you so much. All I wanted to know.
    • Marked as answer by Marvin_Guo Thursday, February 13, 2014 1:10 PM
    • Unmarked as answer by Marvin_Guo Thursday, February 13, 2014 1:10 PM
    Wednesday, February 5, 2014 11:47 PM