ManagementObject and QueryCollection Error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code that works fine on my Development Computer but not
on my other computer. Both Computers are running Windows XP Pro and both
have .netFramework 1.1 installed. The last line of code on my other computer
gives me an error "Item set to Null", probably because the
QueryCollection.Count is returning a null value.

Dim drives() As String
Dim query As ManagementObjectSearcher = New _
ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ")
Dim queryCollection As ManagementObjectCollection = query.Get()
ReDim drives(queryCollection.Count - 1) 'I get the error on this line

Any idea what could be causing this?
 
I cannot see why you want to use the Management Object for for getting
drives. You could just return the drives as an array. Like so:

Imports System.IO

Dim strDrives() As String = Directory.GetLogicalDrives
Dim strDrive As String

For Each strDrive in strDrives
Console.WriteLine(strDrive)
Next

Its a lot easier & will work on both machines
 
Because I study VB.Net as a hobby and want to learn about it's different
aspects...This is the way to learn and it often leads down paths to things
that can't be done the easy way. One example is how do you enumerate the
shared drives and folders? One way is with:

Dim o As Object = CreateObject("WScript.Network")
Dim odrives As Object = o.EnumNetworkDrives

However, this works on one computer and not the other. How would you
enumerate the shared (not network mapped) drives and folders?
 
Back
Top