如何在本地和远程计算机上同时使用Windows Search Service?

时间:2019-04-27 16:45:24

标签: c# oledbconnection oledbcommand windows-search

我正在研究一个C#WPF项目,该项目可以在指定目录路径上搜索文件。这些路径也可以在本地和远程计算机上,因此我必须在两者上都解决它。

最初,我尝试使用具有本地路径的查询,但是OleDbDataReader类返回的记录为零。我确定我已经按照相应的方式调整了所有内容,例如索引选项包含正确的路径:Indexing Options contains the correct path-或在文件浏览器中包含:or in file explorer-文件类型在列表中包含txt文件:File Types contains txt file

本地搜索代码:

CSearchManager manager = new CSearchManager();
CSearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
queryHelper.QuerySelectColumns = "System.ItemName,System.FileName,System.Author,System.ItemUrl,System.ItemType";
queryHelper.QueryWhereRestrictions = @"AND SCOPE='file:D:\testIndexing' AND System.FileName LIKE '*.txt'";
string userQuery = "SELECT System.FileName FROM SystemIndex WHERE SCOPE='file:D:\testIndexing' AND System.FileName LIKE '*.txt'";
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(userQuery);

OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString);
conn.Open();
OleDbCommand command = new OleDbCommand(sqlQuery, conn);
using (OleDbDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        Console.WriteLine(reader.GetString(0));
        Console.WriteLine(reader[0]);
    }
}
conn.Close();

稍后,我尝试连接到Windows Server。服务器路径正确,因为我可以通过文件资源管理器浏览服务器文件-这是VPN连接-。此处也正确调整了索引选项。但是问题是我不能使用上面的连接方法,因为GetCatalog methot不支持远程连接-因此我尝试了THIS方法。我找到了一个可以在下面看到的示例,但是在这里我得到了一个示例: System.UnauthorizedAccessException: 'Retrieving the COM class factory for remote component with CLSID {[CLSID]} from machine [SERVER NAME] failed due to the following error: 80070005 [SERVER NAME].'

问题是当我尝试添加CLSID时。我不知道在哪里可以获取此ID,因为我没有找到任何具体的解决方案。在这里,我需要一些帮助,例如我的情况下的CLSID,以及如何获取它?

远程搜索代码:

Guid guid = new Guid("{[CLSID]}"); // this is what I don't know how to provide
Type managerType = Type.GetTypeFromCLSID(guid, "SERVER_NAME", true);
var comManager = Activator.CreateInstance(managerType);
CSearchManagerClass manager = (CSearchManagerClass)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(comManager, typeof(CSearchManagerClass));

CSearchCatalogManager catalogManager = manager.GetCatalog("SERVER_NAME.SystemIndex");
CSearchQueryHelper queryHelper = catalogManager.GetQueryHelper();
queryHelper.QuerySelectColumns = "System.ItemName,System.FileName,System.Author,System.ItemUrl,System.ItemType";
queryHelper.QueryWhereRestrictions = @"AND SCOPE='file:\\SERVER_NAME\path\to\scope' AND System.FileName LIKE '*.txt'";

string userQuery = @"SELECT System.FileName FROM SystemIndex WHERE SCOPE='file:\\SERVER_NAME\path\to\scope' System.FileName LIKE '*.txt'";
string sqlQuery = queryHelper.GenerateSQLFromUserQuery(userQuery);

OleDbConnection conn = new OleDbConnection(queryHelper.ConnectionString);
conn.Open();
OleDbCommand command = new OleDbCommand(sqlQuery, conn);
using (OleDbDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        Console.WriteLine(reader.GetString(0));
        Console.WriteLine(reader[0]);
    }
}
conn.Close();

0 个答案:

没有答案