Click here to Skip to main content
16,016,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the application that checks a remote machine for spcific information using WMI. i have noticed that when i search the first time it pulls the information. and the second time it searches it still pulls the first machien infomation.

I have a clear command on the search click event where it clears all the data from the fields. But it dosent help its like the first machine name i typed in is stuck in memory and the code itsnt seeing it.

here is my code for wmi. all of this is under a button click and runs in a background worker.

dim strComputer as string = machinenametextbox.text
Dim MyObjSearcher As System.Management.ManagementObjectSearcher
Dim MyColl As System.Management.ManagementObjectCollection
Dim MyObj As System.Management.ManagementObject
Dim MyConOptions As New System.Management.ConnectionOptions
With MyConOptions
    .Impersonation = System.Management.ImpersonationLevel.Impersonate
    .Authentication = System.Management.AuthenticationLevel.Packet
End With
Dim MyMgtScope As System.Management.ManagementScope
MyMgtScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", MyConOptions)
MyMgtScope.Connect()
If MyMgtScope.IsConnected = False Then
    Exit Sub
End If
'----------------------------------------------------------------------------
'Basic machine INnformation for the left column
'----------------------------------------------------------------------------
Try
    MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
            "Select * FROM Win32_OperatingSystem")
    MyColl = MyObjSearcher.Get
    For Each MyObj In MyColl
        OSNAME = (MyObj("caption").ToString())
        If OSNAME = "Microsoft Windows 7 Enterprise " Then
            OSNAME = "Windows 7"
            RadLabel1.Text = "CAE Info"
        ElseIf OSNAME = "Microsoft Windows XP Professional" Then
            OSNAME = "Windows XP"
            RadLabel1.Text = "Radia Info"
            CAE_Tab.Item.Visibility = ElementVisibility.Collapsed
        End If
        BackgroundWorkerMachineInfo.ReportProgress(10)
    Next

Catch ex As Exception
    OSNAME = "UnKnown"
End Try
Try
    MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
            "Select * FROM Win32_OperatingSystem")
    MyColl = MyObjSearcher.Get
    For Each MyObj In MyColl
        SERVICEPACK = (MyObj("servicepackmajorversion").ToString())
        IMAGEDATE = ManagementDateTimeConverter.ToDateTime(CStr(MyObj("InstallDate")))
        LASTREBOOT = ManagementDateTimeConverter.ToDateTime(CStr(MyObj("LastBootUpTime")))
        BackgroundWorkerMachineInfo.ReportProgress(20)
    Next
Catch ex As Exception
    SERVICEPACK = "Unavailable"
    IMAGEDATE = "Unavailable"
    LASTREBOOT = "Unavailable"
End Try
Posted

1 solution

Don't clear them out. Dispose of all the Scope and Searcher objects and create new ones.
 
Share this answer
 
Comments
#realJSOP 1-Mar-12 16:06pm    
5 - proposed as answer
Zachary.shupp 1-Mar-12 20:18pm    
how do i do that? i tried looking up the dispose option and dont really understand it. if you could provide an example that would be great...
Dave Kreskowiak 2-Mar-12 12:44pm    
The Using statements automatically call Dispose on the object thats specified in them when execution leaves the Using block.

Dispose says that you're not using the object any more and that object uses unmanaged resource, like Windows WMI, and the object releases those resources back to Windows. How do you know if you should call Dispose when you're done with something?? It'll have a Disponse method on it.

For example:
<pre> '----------------------------------------------------------------------------
'Basic machine INnformation for the left column
'----------------------------------------------------------------------------
Try
Using MyObjSearcher As New ManagementObjectSearcher(String.Format("\\{0}\root", machineName), "Select * FROM Win32_OperatingSystem")
Using resultSet As ManagementObjectCollection = MyObjSearcher.Get

For Each MyObj In MyColl
OSNAME = MyObj("caption").ToString()
If OSNAME = "Microsoft Windows 7 Enterprise " Then
OSNAME = "Windows 7"
RadLabel1.Text = "CAE Info"
ElseIf OSNAME = "Microsoft Windows XP Professional" Then
OSNAME = "Windows XP"
RadLabel1.Text = "Radia Info"
CAE_Tab.Item.Visibility = ElementVisibility.Collapsed
End If

SERVICEPACK = MyObj("servicepackmajorversion").ToString()
IMAGEDATE = ManagementDateTimeConverter.ToDateTime(CStr(MyObj("InstallDate")))
LASTREBOOT = ManagementDateTimeConverter.ToDateTime(CStr(MyObj("LastBootUpTime")))

BackgroundWorkerMachineInfo.ReportProgress(10)
Next
End Using
End Using
Catch ex As Exception
OSNAME = "UnKnown"
SERVICEPACK = "Unavailable"
IMAGEDATE = "Unavailable"
LASTREBOOT = "Unavailable"
End Try</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900