0

有没有办法用参数调用 SearchIndexer?(或者还有其他方法可以完成标题所说的吗?)

我尝试查看各种 MSDN 文章,但它们似乎都建议我使用库。但是当我运行搜索时,它会运行,而无需我下载任何类型的库。

回到 XP 时代,您可以转到索引服务属性并执行查询。我在 Windows 7 中看不到这一点。

谢谢。

4

1 回答 1

2

这是一个示例查询。请注意,它不使用 Windows 7 SDK。

using System;
using System.Data.OleDb;

namespace FileSearchingExe
{
class MainProgram
{
    static void Main(string[] args)
    {     
string sqlQuery = "SELECT TOP 10 \"System.ItemPathDisplay\", \"System.DateModified\" FROM \"SystemIndex\" WHERE CONTAINS(*,'\"urSearchWord*\"') " +
            "AND scope='file:C:/SomeFolder' ORDER BY System.ItemPathDisplay DESC"; //note the forwardslash in the scope parameter.

        // --- Perform the query ---
        // create an OleDbConnection object which connects to the indexer provider with the windows application
        using (System.Data.OleDb.OleDbConnection conn = new OleDbConnection("provider=Search.CollatorDSO.1;EXTENDED PROPERTIES=\"Application=Windows\""))//queryHelper.ConnectionString))
        {
            // open the connection
            conn.Open();

            // now create an OleDB command object with the query we built above and the connection we just opened.
            using (OleDbCommand command = new OleDbCommand(sqlQuery, conn))
            {
                // execute the command, which returns the results as an OleDbDataReader.
                using (OleDbDataReader WDSResults = command.ExecuteReader())
                {
                    while (WDSResults.Read())
                    {
                        // col 0 is our path in display format

                        Console.WriteLine("{0}, {1}", WDSResults.GetString(0), WDSResults.GetDateTime(1).ToString());
                    }
                }
            }
        }
     }
 }
 }

但是,它改编自 Windows 7 SDK 中的 DSearch 示例。([SDK]\Samples\winui\WindowsSearch\DSearch。[SDK] 通常是“C:\Program Files\Microsoft SDKs\Windows\v7.1”

请注意,如果您使用 SDK 的ISearchQueryHelper. 但是,要使用该类和相关类,您需要参考Microsoft.Search.Interop,它不作为 dll 包含在 Windows 7 SDK 中。但是,您可以通过在 SearchAPI.tlb 文件(在 [SDK]\Lib 中)上使用 TlbImp.exe(类型库导入器,在 [SDK]\bin 中)以 dll 形式获取它。 这里也有描述

我希望这篇文章可以帮助任何需要在 Windows 7 或更高版本中以编程方式连接到 Windows Search 的人。

于 2012-01-13T02:09:22.610 回答