Check if a location is indexed in Windows Search

1.6k Views Asked by At

How to check if a location is indexed or not? I found following code to index a location in Windows which works fine but I want to check if it is indexed or not before I make it indexed.

Uri path = new Uri(location);
string indexingPath = path.AbsoluteUri;

CSearchManager csm = new CSearchManager();
CSearchCrawlScopeManager manager =  csm.GetCatalog("SystemIndex").GetCrawlScopeManager();

manager.AddUserScopeRule(indexingPath, 1, 1, 0);
manager.SaveAll();

Guys i have found a way to check if the location has been included for indexing by using IncludedInCrawlScope.

CSearchManager csm = new CSearchManager();
CSearchCrawlScopeManager manager = csm.GetCatalog("SystemIndex").GetCrawlScopeManager();

if (manager.IncludedInCrawlScope(indexingPath) == 0)
{
    manager.AddUserScopeRule(indexingPath, 1, 1, 0);
    manager.SaveAll();
}

But it only checks if it has been added for indexing, not if the indexing is complete.Since i will be querying on the SystemIndex, i need to make sure that the location is indexed.

2

There are 2 best solutions below

0
On

Check this implementation from a document management system:

https://code.google.com/p/olakedms/source/browse/SearchEngine/CSearchDAL.cs?r=171

0
On

I ran into a similar need and this is what I came up with. In my case I have certain file extensions that are going to end up being sent to a document management system.

I have two methods one uses the System.IO to get a list of the files in the directory that contain the extension from the list.

 public IEnumerable<string> DirectoryScan(string directory)
    {

        List<string> extensions = new List<string>
        {
        "docx","xlsx","pptx","docm","xlsm","pptm","dotx","xltx","xlw","potx","ppsx","ppsm","doc","xls","ppt","doct","xlt","xlm","pot","pps"
        };
        IEnumerable<string> myFiles =
            Directory.GetFiles(directory, "*", SearchOption.AllDirectories)
                .Where(s => extensions.Any(s.EndsWith))
                .ToList();
        return myFiles;
    }`

The second method uses the windows index search Microsoft.Search.Interop

 public IEnumerable<string> QueryWindowsDesktopSearch(string directory)
    {

        List<string> extensions = new List<string>
        { "docx","xlsx","pptx","docm","xlsm","pptm","dotx","xltx","xlw","potx","ppsx","ppsm","doc","xls","ppt","doct","xlt","xlm","pot","pps"};

        string userQuery = "*";
        Boolean fShowQuery = true;
        List<string> list = new List<string>();
        CSearchManager manager = new CSearchManager();
        CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
        CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
        queryHelper.QueryWhereRestrictions = string.Format("AND (\"SCOPE\" = 'file:{0}')", directory);

        if (extensions != null)
        {
            queryHelper.QueryWhereRestrictions += " AND Contains(System.ItemType,'";
            bool fFirst = true;
            foreach (string ext in extensions)
            {
                if (!fFirst)
                {
                    queryHelper.QueryWhereRestrictions += " OR ";
                }
                queryHelper.QueryWhereRestrictions += "\"" + ext + "\"";
                fFirst = false;
            }
            queryHelper.QueryWhereRestrictions += "') ";
        }

        string sqlQuery = queryHelper.GenerateSQLFromUserQuery(userQuery);

        using (OleDbConnection connection = new OleDbConnection(queryHelper.ConnectionString))
        {
            using (OleDbCommand command = new OleDbCommand(sqlQuery, connection))
            {
                connection.Open();
                OleDbDataReader dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    var file = dataReader.GetString(0);
                    if (file != null)
                    {
                        list.Add(file.Replace("file:", ""));
                    }
                }
            }
        }
        return list;
    }

I call both of these methods from another methods that takes the two results and compares them and returns a Boolean value indicating if they two list match. If they do not match then the folder has not been indexed fully.

If you call the QueryWindowsDesktopSearch on a folder that has not been indexed it returns zero files. You could use this as an indication that the folder isn't in the index bt its possible that the file has been added to the index but the file indexing is stopped.

You could check the status by calling something like this

 CSearchManager manager = new CSearchManager();
 CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
 _CatalogPausedReason pReason;
 _CatalogStatus pStatus;
 catalogManager.GetCatalogStatus(out pStatus, out pReason);

That may return something like pStatus = CATALOG_STATUS_PAUSED and pReason = CATALOG_PAUSED_REASON_USER_ACTIVE

You would know that the index is not running. Another thing you could do is call the following

int incrementalCount, notificationQueue, highPriorityQueue;
catalogManager.NumberOfItemsToIndex(out incrementalCount, out notificationQueue, out highPriorityQueue);

This is going to return the in plIncrementalCount value which would list the number of file that the entire SystemIndex has queued for indexing.