You are on page 1of 8

Managing Windows Indexing Service with

Visual Basic.NET using WMI


(Page 1 of 6 )

This article explains how to manage the Windows Indexing Service using the .NET framework.
We will manage the Indexing Service using WMI (together with .NET).
The sample downloadable solution (zip) is entirely developed using Visual Studio.NET 2003
Enterprise Architect on Windows Server 2003 Standard Edition. But, I am confident that it
would work with other versions of Windows (which support .NET 1.1) as well.
Introduction to Indexing Service
"Indexing Service" is not a new service. It has been available since Windows 2000 (I am not sure
about Windows NT). You can use Indexing Service to index documents and document properties
on your disks and store the information in a "catalog." You can also use Indexing Service to
search for documents, either through Search on the Start menu or through a web browser. It also
allows users to perform fast full-text searches.
Indexing Service in Windows is also a favorite for many web application developers (especially
ASP.NET developers), as they can implement "search site" within their website (or web
application) very easily. Most of all, it also supports "SELECT" queries against indexed
information available in catalogs.
How do you check whether the Indexing Service exists on your system? Just go to
Administrative Tools -> Services. And within the list of several services, you should see
"Indexing Service." If you are unable to find it, then I can confirm that the component is not
installed on your system.
How do you get it installed? Just open "Add/Remove Programs" in "Control Panel" and within
the dialog box, click on "Add/Remove Windows Components." You will be presented with a list
of all Windows components. The components that are checked are already installed on your
computer. So, make sure that "indexing service" is checked (as shown in Fig1) and click "Next"
to proceed through installation. It may ask for Windows OS CD if necessary.
Managing Windows Indexing Service with
Visual Basic.NET using WMI - How to work
with/manage Indexing Service
(Page 2 of 6 )

Those who are familiar with "Indexing Service" can skip this section. How do you start/stop an
"indexing service"? Just go to Administrative Tools -> Services. Within the list of services, find
"Indexing Service" and right click on it.
You have several options within that menu. You can start/stop or even go to the properties.
Within the properties you can modify the "start mode" to "automatic" (meaning that it is
automatically started when the OS starts) or "manual" or even "disabled." Once "disabled", the
service could never be started unless you change the mode back to "automatic" or "manual."
Your next questions would probably be something like "Where are all the catalogs?", "How
much indexing has it completed?", "Is it still indexing?" etc. Just a simple window will answer
all of those questions.
Go to Administrative Tools -> Computer Management. In the left pane, open "Services and
Applications" and select "Indexing Service" (Fig 2). You will see a list of all existing catalogs at
the right side pane along with their indexing status and other criteria. Everything can be
operated with "right click," which includes "adding new catalogs," "deleting catalogs," "starting
or stopping indexing operations," and so on. Just try to play with all of those options (don't
worry, nothing harms your computer!)

Managing Windows Indexing Service with


Visual Basic.NET using WMI - Getting
"Indexing service" information with WMI
and VB. NET
(Page 3 of 6 )
Can we manage/access "Indexing service" directly from within .NET (natively)? .NET natively
doesn't support access to Indexing service. So, we need to extend our .NET application to
interact with either WMI or COM to manage/access the "indexing service."
WMI is a good emerging technology from Microsoft for their support to WBEM (Web Based
Enterprise Management). For a complete understanding of WMI together with .NET
programming, I suggest you go through my series "WMI programming using Visual
Basic.NET." Once you are familiar with at least the first two parts in that series, there is no doubt
that you will be able to understand this article easily.
The downloadable solution contains a "WinForms" application, which interacts with the
"Indexing Service" using pure WMI. It is important to note that you need to add a reference to
"System.Management" namespace to your project, when you are working with WMI (and also
import it at the top).
Let us go through a small code fragment, which I used to retrieve information from an "Indexing
Service."
Private Sub btnCaption_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnCaption.Click
Dim LateBoundObject As ManagementObject
LateBoundObject = New ManagementObject(Nothing, New
ManagementPath
("\\SERVER\root\CIMV2:Win32_Service.Name=""CiSvc"""), Nothing)
MessageBox.Show(CType (LateBoundObject("caption"),
String))
LateBoundObject.Dispose()
End Sub
Within the above code, I am creating a "ManagementObject" (an object related to WMI) and
then assigning the path of the indexing service ("CiSvc") object to the "ManagementObject".
You can also find/list all the services along with their names using WMI and Visual Basic.NET
(which is demonstrated in "WMI programming using Visual Basic.NET" series). For now, we
concentrate only on the "indexing service."
The WMI class "Win32_Service" in "root\CIMV2" namespace has a property "caption", which
gives the title/caption of that service. To retrieve that, I simply used the property "caption" with
the management object created. And finally I disposed the "ManagementObject".

Managing Windows Indexing Service with


Visual Basic.NET using WMI - Getting a few
other properties of "Indexing service" with
WMI and VB. NET
(Page 4 of 6 )

Once you understand the previous section, there is no doubt that you can understand this section
very easily. So, let us go through a few more scripts from the sample solution you downloaded.
The following script informs you whether the "indexing service" has been started or not.
Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnStatus.Click
Dim LateBoundObject As ManagementObject
LateBoundObject = New ManagementObject(Nothing, New
ManagementPath
("\\SERVER\root\CIMV2:Win32_Service.Name=""CiSvc"""), Nothing)
MessageBox.Show(CType(LateBoundObject("Started"),
Boolean))
LateBoundObject.Dispose()
End Sub
The above script is very similar to the one provided in the previous section except that I used the
property "Started." The property just returns "Boolean" (whether the service started or not).
The following script informs you of the "start mode" of the "indexing service" (Automatic,
Manual or Disabled).
Private Sub btnStartMode_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnStartMode.Click
Dim LateBoundObject As ManagementObject
LateBoundObject = New ManagementObject(Nothing, New
ManagementPath
("\\SERVER\root\CIMV2:Win32_Service.Name=""CiSvc"""), Nothing)
MessageBox.Show(CType(LateBoundObject("StartMode"),
String))
LateBoundObject.Dispose()
End Sub
And I hope you can understand the above code fragment very easily. I just changed the "Started"
property to "StartMode".

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.

Managing Windows Indexing Service with


Visual Basic.NET using WMI - Passing input
parameters to WMI for managing Indexing
Service
(Page 6 of 6 )
In the previous section, we dealt with some of the WMI methods that don't have any input
parameters. But, there will be cases where input parameters are necessary. One of these cases is
"changing the start mode" of the indexing service.
To change the start mode, we need to provide the type of start mode to WMI (which is an input
parameter). This input parameter will be gathered by the WMI method; it manages the indexing
service accordingly.
The following script changes/modifies the start mode of indexing service.
Private Sub btnChangeStartMode_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnChangeStartMode.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
inParams = LateBoundObject.GetMethodParameters
("ChangeStartMode")
inParams("StartMode") = "Automatic"
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("ChangeStartMode", inParams,
Nothing)
MessageBox.Show(outParams.Properties
("ReturnValue").Value.ToString)
LateBoundObject.Dispose()
End Sub
You could observe that everything is the same, except for some new lines as follows:
inParams = LateBoundObject.GetMethodParameters("ChangeStartMode")
inParams("StartMode") = "Automatic"
Dim outParams As System.Management.ManagementBaseObject =
LateBoundObject.InvokeMethod("ChangeStartMode", inParams,
Nothing)
From the above statements, we can understand that we are working with the WMI method
"ChangeStartMode" which accepts an input parameter called "StartMode" (which is of type
string). First of all, we get all parameters into "inParams" with the help of
"getMethodParameters", focussing on "ChangeStartMode" method. Then, we assign values for
all those input parameters in the form of properties. Finally we invoke the WMI method using
"InvokeMethod".
Summary
WMI leads the way to WBEM (Web Based Enterprise Management) in a very easy and pleasant
manner. I focussed only on "Indexing Service" in this article, to work with WMI. But you can
work with almost any service within Windows OS. Another great point is that WMI is
transparent to ASP.NET as well. Try to convert the above application to a web application
(ASP.NET). It works without any core changes!
One should also note that WMI does not supercede all of the features of every service (or the
entire Win32 API). At the same time, WMI is getting enhanced and extended day by day. For
example, we cannot work with "catalogs" in an indexing service using WMI. WMI does not
support it (though it may be supported in future versions of WMI). To work with "catalogs" of
the indexing service, we need to work with COM (you can expect my next article on it soon).
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at
jag_chat@yahoo.com.

You might also like