Enable Windows Search Indexing on Folders

February 8, 2009

one comment

The Windows Search, introduced in Vista as part of the core operating system, is an extremely valuable tool in my pocket.  I barely ever use the start menu, ever – and why would I, if every program is just a few keys on the keyboard away?

image

Windows Search indexing (for faster search results) is enabled by default on most of the locations you’d want it to index, but what if your app has a particular scenario in which it uses Windows Search but wants to use it to search non-indexed locations?

The ability to change the indexed locations might come in handy in these scenarios, and fortunately there’s a relatively simple set of COM interfaces that can be used to interact with Windows Search.

The following code can determine whether a location is indexed:

/// <summary>

/// Determines whether the specified location is currently indexed.

/// </summary>

/// <param name="location">The location.</param>

/// <returns>Whether the location is indexed.</returns>

public static bool IsLocationIndexed(string location)

{

    CSearchManager searchManager = new CSearchManager();

    CSearchCatalogManager catalogManager = searchManager.GetCatalog("SystemIndex");

    CSearchCrawlScopeManager scopeManager = catalogManager.GetCrawlScopeManager();

 

    int result = scopeManager.IncludedInCrawlScope(location);

 

    Marshal.ReleaseComObject(scopeManager);

    Marshal.ReleaseComObject(catalogManager);

    Marshal.ReleaseComObject(searchManager);

 

    return result != 0;

}

The following simple code can be used to enable indexing (of course, after asking for the user’s consent!) on a specified folder:

/// <summary>

/// Adds the specified locations to the system index.

/// </summary>

/// <param name="locations">The locations to add.</param>

public static void IndexLocations(params string[] locations)

{

    CSearchManager searchManager = new CSearchManager();

    CSearchCatalogManager catalogManager = searchManager.GetCatalog("SystemIndex");

    CSearchCrawlScopeManager scopeManager = catalogManager.GetCrawlScopeManager();

 

    foreach (string location in locations)

    {

        string url = location;

        if (url[url.Length – 1] != ‘\\’)

            url += ‘\\’;

        if (!url.StartsWith("file:///"))

            url = "file:///" + url;

 

        scopeManager.AddUserScopeRule(url, 1, 0, 0);

    }

    scopeManager.SaveAll();

 

    Marshal.ReleaseComObject(scopeManager);

    Marshal.ReleaseComObject(catalogManager);

    Marshal.ReleaseComObject(searchManager);

}

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. SnekithanOctober 17, 2013 ב 4:21 PM

    What is the assembly (or) COM component needed to be included in order reference, in order to use CSearchCrawlScopeManager class in C# project?

    Thanks

    Reply