gpt4 book ai didi

c# - 检查某个位置是否已在 Windows 搜索中编入索引

转载 作者:可可西里 更新时间:2023-11-01 09:39:17 24 4
gpt4 key购买 nike

如何检查一个位置是否被索引?我发现以下代码可以在 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 上查询,因此我需要确保该位置已被索引。

最佳答案

我遇到了类似的需求,这就是我想到的。就我而言,我有某些文件扩展名最终将被发送到文档管理系统。

我有两种方法,一种是使用 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;
}

我从另一个方法调用这两个方法,该方法获取两个结果并比较它们并返回一个 bool 值,指示它们是否两个列表匹配。如果它们不匹配,则该文件夹没有被完全索引。

如果您在尚未编制索引的文件夹上调用 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 已排队等待索引的文件数。

关于c# - 检查某个位置是否已在 Windows 搜索中编入索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com