Public Function GetDrive(ByVal DriveType As String, ByRef ErrMsg As String) As DataTable
Dim dt As New DataTable, dr As DataRow
Dim myScope As New ManagementScope("\\" & Environment.MachineName & "\root\cimv2")
Dim oQuery As New SelectQuery("SELECT DeviceID, DriveType, ProviderName FROM win32_logicaldisk")
Dim oResults As New ManagementObjectSearcher(myScope, oQuery)
Dim oItem As ManagementObject
Dim oProperty As PropertyData
dt.Columns.Add("DriveLetter", GetType(String))
dt.Columns.Add("Path", GetType(String))
Try
For Each oItem In oResults.Get()
If oItem.Properties.Item("DriveType").Value.ToString = DriveType Then
dr = dt.NewRow()
'Local Drive
If DriveType = "3" Then
dr.Item("DriveLetter") = oItem.Properties.Item("DeviceID").Value.ToString & "\"
Else 'Network Drive
dr.Item("DriveLetter") = oItem.Properties.Item("DeviceID").Value.ToString
dr.Item("Path") = oItem.Properties.Item("ProviderName").Value.ToString
End If
dt.Rows.Add(dr)
End If
Next
Catch ex As Exception
ErrMsg = "* " & ex.Message
Finally
oResults.Dispose()
oItem.Dispose()
myScope = Nothing
oQuery = Nothing
oResults = Nothing
oItem = Nothing
oProperty = Nothing
End Try
Return dt
End Function