Managing Windows Indexing Service with Visual Basic.NET using WMI - Managing "Indexing service" with WMI and VB. NET
(Page 5 of 6 )
Until now, in previous sections, I simply explained how you could retrieve information from an "indexing service". Now we shall "manage" the "indexing service".
The following code starts the indexing service:
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click
Dim LateBoundObject As ManagementObject
LateBoundObject = New ManagementObject(Nothing, New
ManagementPath
("\\SERVER\root\CIMV2:Win32_Service.Name=""CiSvc"""), Nothing)
Dim inParams As System.Management.ManagementBaseObject =
Nothing
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("StartService", inParams, Nothing)
MessageBox.Show(outParams.Properties
("ReturnValue").Value.ToString)
LateBoundObject.Dispose()
End Sub
The above code features a few new declarations, especially "inParams" and "outParams". When we need to pass some information to a WMI method (of a WMI class), we pass it using an object related to "ManagementBaseObject" (in this case "inParams"). The WMI method may return some information back to our application, which will again be a "ManagementBaseObject" (in this case "outParams"). We execute the WMI method using the "InvokeMethod" method available in the "ManagementObject" class.
From the above code, we need to understand that I received no input parameters to supply for the WMI method "StartService" (and thus it is "nothing"). Once the WMI method "StartService" is executed through "InvokeMethod", the result may be available in "outParams". And we retrieve the "ReturnValue" using same property available in "outParams".
Let us go through a few other lines of coding available. The following line (if replaced) stops the indexing service.
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("StopService", inParams, Nothing)
The following line (if replaced) pauses the indexing service.
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("PauseService", inParams, Nothing)
The following line (if replaced) resumes the indexing service.
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("ResumeService", inParams, Nothing)
So you can see how easy it is to work with WMI methods.
Next: Passing input parameters to WMI for managing Indexing Service >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee