I am getting the same error. This appears to be a bug. My code is just a
Windows Form. The error does not occur on my XP Professional system, but
does on my Windows 2003 Server (includes 1.1). Here is the code:
Public Sub New(ByVal sComputerName As String)
If sComputerName = "(local)" Then
sComputerName = Environment.MachineName
End If
moScope = New ManagementScope("\\" + sComputerName + "\root\cimv2")
End Sub
Public Function RetrieveFixedDrives() As String()
Dim oSelectQuery As SelectQuery
Dim iDriveType As Integer
Dim sDrives(0) As String
Dim oSearcher As ManagementObjectSearcher
Dim oMgmtObjColl As ManagementObjectCollection
Dim oDisk As ManagementObject
Dim iCount As Integer
Try
oSelectQuery = New SelectQuery("win32_logicalDisk")
oSearcher = New ManagementObjectSearcher(moScope, oSelectQuery)
If oSearcher Is Nothing Then
Exit Function
End If
oMgmtObjColl = oSearcher.Get
'this "appears" to work and does not throw the exception
ReDim sDrives(oMgmtObjColl.Count)
iCount = 0
'Here is where it will fail
For Each oDisk In oMgmtObjColl
iDriveType =
Integer.Parse(oDisk.Properties("DriveType").Value. ToString)
If iDriveType = 3 Then
sDrives(iCount) =
oDisk.Properties("DeviceID").Value.ToString()
iCount += 1
End If
Next oDisk
Return sDrives
Catch ex As Exception
MsgBox(ex.Message.ToString & ": " & ex.InnerException.ToString &
": " & ex.StackTrace)
End Try
End Function
I fixed the issue by rewriting the code this way:
Public Sub New(ByVal sComputerName As String)
If sComputerName = "(local)" Then
sComputerName = Environment.MachineName
End If
moScope = New ManagementScope("\\" + sComputerName + "\root\cimv2")
End Sub
Public Function RetrieveFixedDrives() As String()
Dim oSelectQuery As SelectQuery
Dim iDriveType As Integer
Dim sDrives(0) As String
Dim oSearcher As ManagementObjectSearcher
Dim oMgmtObjColl As ManagementObjectCollection
Dim oDisk As ManagementObject
Dim iCount As Integer
Try
oSelectQuery = New SelectQuery("win32_logicalDisk")
oSearcher = New ManagementObjectSearcher(moScope, oSelectQuery)
If oSearcher Is Nothing Then
Exit Function
End If
iCount = 0
For Each oDisk In oSearcher.Get()
iDriveType =
Integer.Parse(oDisk.Properties("DriveType").Value. ToString)
If iDriveType = 3 Then
ReDim Preserve sDrives(iCount)
sDrives(iCount) =
oDisk.Properties("DeviceID").Value.ToString()
iCount += 1
End If
Next oDisk
Return sDrives
Catch ex As Exception
MsgBox(ex.Message.ToString & ": " & ex.InnerException.ToString &
": " & ex.StackTrace)
End Try
End Function
"Willy Denoyette [MVP]" wrote:
What version of the framework are you running?
Is this a Windows Forms application?
Willy.
"Ian Frawley" <ch****@away.com> wrote in message
news:Od***************@news-1.opaltelecom.net... ManagementObjectCollection.Count is it rubbish or what?
I got one of the weirdest exception due to it:
COM object that has been separated from its underlying RCW can not be
used.
Weird
--
"Life should NOT be a journey to the grave
with the intention of arriving safely in an
attractive and well preserved body,
but rather to skid in sideways,
chocolate in one hand, beer in the other,
body thoroughly used up,
totally worn out and screaming
WOO HOO what a ride!"