Question

Is there a way to call SearchIndexer with arguments? (or is there another way to accomplish what the title says?)

I tried looking at the various MSDN articles, but they all seemed to suggest that I use a library. But when I run the search, it runs, without me downloading any sort of library.

Back in the days of XP, you could go to the indexing service properties and execute a query. I don't see that in Windows 7.

Thanks.

Was it helpful?

Solution

Here's an example query. Note that it does not use the 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());
                    }
                }
            }
        }
     }
 }
 }

However, it was adapted from the DSearch example in the Windows 7 SDK. ([SDK]\Samples\winui\WindowsSearch\DSearch. [SDK] is typically "C:\Program Files\Microsoft SDKs\Windows\v7.1"

Note that you can make the SQL query more easily (but slightly less flexible imo) if you use the SDK's ISearchQueryHelper. To use that class and related classes though, you need have reference to Microsoft.Search.Interop, which is not included in the Windows 7 SDK as a dll. You can however get it in dll form by using TlbImp.exe (type library importer, in [SDK]\bin) on the SearchAPI.tlb file (in [SDK]\Lib). Also described here.

I hope this post helps anyone else who needs to programmatically connect to the Windows Search in Windows 7 or higher.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top