0

有沒有辦法使用參數調用SearchIndexer? (或有另一種方式來完成標題說什麼?)以編程方式搜索Windows 7而不使用sdk庫

我試着看着各種MSDN文章,但他們似乎都建議我使用庫。但是當我運行搜索,它運行,沒有我下載任何類型的庫。

早在XP的時代,您可以轉到索引服務屬性並執行查詢。我沒有看到在Windows 7中。

謝謝。

回答

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,它不包含在Windows 7 SDK中作爲一個dll。在SearchAPI.tlb文件([SDK] \ Lib)中使用TlbImp.exe(類型庫導入程序,位於[SDK] \ bin中)Also described here

我希望本文能夠幫助任何需要以編程方式連接到在Windows 7或更高版本中的Windows搜索。