Managing Windows Indexing Service with Visual Basic.NET Using COM

This article explains how to manage the Windows Indexing Service using the .NET framework. We will manage the Indexing Service using COM (together with .NET).

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
October 26, 2005
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable file for this article is available here.

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) versions as well.

Comparing WMI and COM to work with indexing service using .NET

I already introduced you to the “Indexing Service” in my previous article named “Managing Windows Indexing Service with Visual Basic.NET using WMI.”  In that article, I mainly focused on WMI to communicate with Indexing Service.  It also introduced you to checking the status of Indexing service, installing, managing, and so on.

So, I will not be covering any of those concepts in this article.  If you have any problem with Indexing service, I suggest you go through my previous article.  In this article, I am going to focus on using COM to interact with Indexing Service, using .NET COM interoperability. 

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 of WBEM (Web Based Enterprise Management). WMI gets enhanced day by day.  For a complete understanding of WMI together with .NET programming, I suggest you  go through my series “WMI programming using Visual Basic.NET” at this site.

Before proceeding further one should make a note that .NET interacting with COM makes the application “unmanaged.”  This is a very important point to consider before we proceed further.  If our application works within the limits of the .NET CLR (Common Language Runtime), it is considered “managed.”  But COM operates beyond the control of CLR and thus becomes unmanaged.  Even though COM existed before the evolution of .NET, .NET indeed supports COM interoperability.  So, any exception thrown from within our application may not be only for .NET.  It could also be from COM.

The next question would be, why did I choose COM to work with Indexing Service?  Basically, Indexing Service existed even before the evolution of .NET  and thus has been developed with COM/DCOM support (of course even with COM+ support).  At the moment, Microsoft hasn't yet redesigned using .NET.  So, I have no choice but to work with COM to interact with Indexing Service at the moment (apart from WMI, which I explained in my previous article). 

Even though I can interact with Indexing Service using WMI and .NET natively, WMI didn’t expose too many features to really “penetrate” into all the features of indexing service.  So, in this article, I will try to cover what I did in my previous article, but with COM interoperability (instead of WMI) and further dig the indexing service details to the maximum limit (where WMI could not be reached).

Getting “Indexing service” status information with COM and VB.NET

The downloadable solution contains a “WinForms” application, which interacts with “Indexing Service” using COM (interoperating with .NET).  It is important to note that, you need to add a reference to “Indexing Service Administration Type Library 1.0” (from COM tab) to your project, when you are working with Indexing Service using COM.  You should see a “dll” named “Interop.CIODMLib.dll” within your “bin” folder, which works like a “.NET wrapper,” which interoperates with “Indexing Service.”

Let us go through a small code fragment, which I used to check the status of an “Indexing Service.”

Dim obj As New CIODMLib.AdminIndexServerClass
 
    Private Sub btnStatus_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnStatus.Click
        MessageBox.Show("Machine? " & obj.MachineName)
        MessageBox.Show("Running? " & obj.IsRunning)
        MessageBox.Show("Paused? " & obj.IsPaused)
    End Sub

The “AdminIndexServerClass” within the “CIODMLib” namespace is used to get the information about the status of Indexing Service.  In the above coding, I used “messageboxes” to make it simple.  You can use any “label” or “checkbox” or “radio buttons” for a better look and feel. 

You can also observe that the above code is totally different from what I used with WMI (in my previous article).  I don’t think that I need to explain much about the above code anymore.  It is as simple as that.

How to change the “start mode” of (or manage) indexing service

“Starting” the indexing service is different and “start mode” is different.  “Start mode” is the mode which makes the service “auto start” or “manual start” or even be “disabled.”  We can change “Start mode” directly using the “services snap-in” provided by the Microsoft OS.  Now we shall see how to change the same dynamically using COM programming together with .NET.

Dim obj As New CIODMLib.AdminIndexServerClass

Private Sub btnConfigure2AutoStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnMakeItAutoStart.Click
        obj.EnableCI(True)
        MessageBox.Show("Made it Autostart")
    End Sub
 
Private Sub btnMakeItManualStart_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnMakeItManualStart.Click
        obj.EnableCI(False)
        MessageBox.Show("Made it Manual")
    End Sub

You can observe that the above code got split into two events, one each for “AutoStart” and “ManualStart.”  The “AdminIndexServerClass” mainly contains a method “EnableCI,” which can be used to change the “start mode” of Indexing Service.  It accepts a “Boolean” value (true or false).  A “true” value makes the Indexing Service “auto-start” and vice versa with “false.”

Now let us see how to change the state of Indexing Service.  I would like to “start” or “stop” the indexing service dynamically through programming.  Let us see how.

Dim obj As New CIODMLib.AdminIndexServerClass
 
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click
        obj.Start()
        MessageBox.Show("Service Started ")
    End Sub
 
    Private Sub btnStop_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnStop.Click
        obj.Stop()
        MessageBox.Show("Service Stopped")
    End Sub

I don’t think I need to explain anything about the above code.  I think you can understand that all of the above code (in this section) is also quite different from that of WMI code in my previous article.

How to program the index catalogs

Make sure that “catalog” is totally different from the English word “Catalogue.”  Don’t try to mistake me in the spelling.  Now the interesting concept is working with “catalogs.”  This is not supported by WMI yet.  Now you can see the advantage of using COM (which is the Indexing Service itself).  If we wanted to view the catalog information or work (or manage) catalog information, it is a must to work with COM (at the moment).

Now let us go through the code, which gives us the list of all catalogs available for indexing.

Dim obj As New CIODMLib.AdminIndexServerClass
 
Private Sub btnListCatalogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListCatalogs.Click
        Dim isFoundCatalog As Boolean = obj.FindFirstCatalog
        Dim sCatalogList As String
        While isFoundCatalog
            Dim Catalog As CIODMLib.ICatAdm = obj.GetCatalog
            sCatalogList &= "Name: " & Catalog.CatalogName & ", "
            sCatalogList &= "Loc: " & Catalog.CatalogLocation &
", "
            sCatalogList &= "Running: " & Catalog.IsCatalogRunning & ControlChars.NewLine
            isFoundCatalog = obj.FindNextCatalog()
        End While
        MessageBox.Show(sCatalogList)
    End Sub

“FindFirstCatalog”, “FindNextCatalog” and “GetCatalog” are the methods available in “AdminIndexServiceClass”, which are specially used to retrieve “catalog” information from “Indexing service”.  “FindFirstCatalog” and “FindNextCatalog” returns “true” if they find any catalog.  We can retrieve the currently found catalog using “GetCatalog”, which returns a reference to an object of type “ICatAdm” interface.  After getting the reference, you can play with properties “CatalogName”, “CatalogLocation”, “IsCatalogRunning” and so forth.

Now let us examine how we add new catalogs or delete existing catalogs:

Dim obj As New CIODMLib.AdminIndexServerClass
 
Private Sub btnAddCatalog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCatalog.Click
        obj.Stop()
        obj.AddCatalog("All ASP.NET articles", "D:\Library\DotNet\DotNetArticles\ASP.NET")
        obj.Start()
        MessageBox.Show("Succesfully Added")
    End Sub
 
Private Sub btnDelCatalog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelCatalog.Click
        obj.Stop()
        obj.RemoveCatalog("All ASP.NET articles")
        obj.Start()
        MessageBox.Show("Succesfully Deleted")
    End Sub

I introduced two new methods, “AddCatalog” and “RemoveCatalog” of the same class “AdminIndexServerClass”.  “AddCatalog” takes “catalog name” and “location” as parameters to add a new catalog to the indexing service.  “RemoveCatalog” takes only “catalog name” to remove it from Indexing Service.  One should understand that, before adding or removing a “catalog” from an Indexing Service, it is always recommended to follow a “stop-do-start” procedure.  And just observe how easy the above code is to understand.

Summary

I contributed this article especially to introduce you to .NET compatibility in interoperating with old Microsoft technologies like COM/DCOM and so on, and to penetrate further into managing the Indexing Service.  Even though I suggest you use WMI in certain scenarios, there are still some situations which WMI could not support.  This article confirms it. 

Finally, if you ask me which is best (WMI or COM) to work with “Indexing Service”, I would suggest you implement both.  You work with COM if and only if you could not achieve the same things using WMI.  The order I suggest for picking the right path would be something like the following:

  • Native .NET support
  • WMI support
  • Web Service support
  • COM support

Any comments, suggestions, bugs, errors, discussions, feedback etc. are highly appreciated at jag_chat@yahoo.com.

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials