检查位置是否在Windows搜索中编入索引

时间:2014-05-26 09:13:20

标签: c# windows windows-search

如何检查某个位置是否已编入索引?我找到了以下代码来索引Windows中的一个位置工作正常,但我想在将其编入索引之前检查它是否已编入索引。

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();

伙计们我找到了一种方法来检查是否已使用IncludedInCrawlScope包含该位置以进行索引。

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

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

但它只检查是否已添加索引,而不是索引是否完整。因为我将查询SystemIndex,我需要确保该位置已编入索引。

2 个答案:

答案 0 :(得分:0)

我遇到了类似的需求,这就是我想出来的。在我的情况下,我有一些文件扩展名,最终将被发送到文档管理系统。

我有两个方法,一个使用System.IO来获取目录中包含列表扩展名的文件列表。

 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;
    }`

第二种方法使用windows索引搜索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;
    }

我从另一个方法调用这两个方法,这两个方法接受两个结果并比较它们并返回一个布尔值,指示它们是否两个列表匹配。如果它们不匹配,则表示该文件夹尚未完全编入索引。

如果在未编入索引的文件夹上调用QueryWindowsDesktopSearch,则返回零文件。您可以使用此作为指示文件夹不在索引中bt可能已将文件添加到索引但文件索引已停止。

您可以通过调用此类内容来检查状态

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

这可能会返回类似pStatus = CATALOG_STATUS_PAUSED和pReason = CATALOG_PAUSED_REASON_USER_ACTIVE

的内容

您会知道索引未运行。你可以做的另一件事是调用以下

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

这将返回plIncrementalCount值,该值将列出整个SystemIndex排队等待索引的文件数。

答案 1 :(得分:-1)