如何在 C# 中使用 Windows 搜索服务 [英] How to use Windows Search Service in c#

查看:17
本文介绍了如何在 C# 中使用 Windows 搜索服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,用户可以在本地计算机或网络上搜索文件或文件夹.我正在使用 DirectoryInfo.GetDirecotories().

I'm working on an application, an user can search for files or folders either on the local computer or on the network. I am using DirectoryInfo.GetDirecotories().

  1. 但我也想添加 windows 7 用于搜索的功能,我相信使用索引.我还在 msdn 上看到了 Windows 搜索服务,但我不确定哪种方式最好:查询索引目录或使用搜索服务.有什么建议吗?
  2. 我想知道是否有人可以在 C# 中给我一个搜索索引目录的小例子.
  1. But I also want to add the functionality that windows 7 uses for searching, I believe that uses indexing. I also saw Windows Searching Service on msdn, but I'm not sure which way is best: querying the indexed catalog or using the search service. Any suggestions?
  2. I was wondering if anyone can give me a small example in C# that searches the indexed catalog.

提前致谢!

推荐答案

看下面的例子:

static void Main(string[] args)
{
    var connection = new OleDbConnection(@"Provider=Search.CollatorDSO;Extended Properties=""Application=Windows""");

    // File name search (case insensitive), also searches sub directories
    var query1 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE scope ='file:C:/' AND System.ItemName LIKE '%Test%'";

    // File name search (case insensitive), does not search sub directories
    var query2 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE directory = 'file:C:/' AND System.ItemName LIKE '%Test%' ";

    // Folder name search (case insensitive)
    var query3 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE scope = 'file:C:/' AND System.ItemType = 'Directory' AND System.Itemname LIKE '%Test%' ";

    // Folder name search (case insensitive), does not search sub directories
    var query4 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE directory = 'file:C:/' AND System.ItemType = 'Directory' AND System.Itemname LIKE '%Test%' ";           

    connection.Open();

    var command = new OleDbCommand(query4, connection);

    using (var r = command.ExecuteReader())
    {
        while (r.Read())
        {
            Console.WriteLine(r[0]);
        }
    }

    connection.Close();

    Console.ReadKey();
}

它使用 OLE DB api 连接到索引器服务,并使用类似 SQL 的语法在其 SystemIndex 表中搜索 System 对象.您有 4 个执行不同操作的示例查询.所有示例查询都将在 c: 文件夹中搜索名称中包含 Test 的项目.

It's using OLE DB api to connect to the indexer service and use a SQL-like syntax to search System objects in its SystemIndex table. You have 4 example queries which do different things. All example queries will search in the c: folder for items that contain Test in their names.

您可以在本地或其他机器上搜索文件、文件夹邮件和可能的其他媒体(取决于操作系统).从我研究的内容来看,不支持网络驱动器,因为它们无法被索引,但您可以连接到我假设在后台使用 RPC 的其他机器,这意味着您必须使用不同的 api(例如 System.净).

You can search for files, folders mails and possibly other media (depending on OS) on local or other machines. From what I have researched network drives are not supported since they can't be indexed but you can connect to other machines for which I assume RPC is used in the background which means you have to supply network credentials using a different api (e.g. System.Net).

请注意,要使这些工作正常进行,您的索引必须在目标机器上完全运行(默认情况下).api 对应于您在索引选项中指定的任何内容.这是有问题的屏幕:

Note that for any of this to work your indexing must be fully operational on the target machine (which it is by default). The api is corresponding to whatever you specify in your Indexing Options. This is the screen in question:

可以在此处找到 System 对象的完整属性列表:属性系统参考.该对象包含诸如 URL、路径、名称、日期等内容.

The full list of properties for the System object can be found here: Property System Reference. This object contains things such as URL, Path, Name, Date etc.

可以在此处找到使用不同谓词(例如 scopedirectory)的更多有趣示例:Windows Vista 搜索语法.还有一个粗略的 MSDN 文档:范围和目录谓词

More interesting examples using different predicates (e.g. scope and directory) can be found here: Windows Vista Search Syntax. There is also a crude MSDN documentation: SCOPE and DIRECTORY Predicates

我建议你查看文档,因为你可以用这个 API 做很多的事情.

I recommend you check out the documentation because you can do a lot of stuff with this api.

这篇关于如何在 C# 中使用 Windows 搜索服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆