Professional Documents
Culture Documents
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!)
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".
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.