Search code examples
c#windowssearchwds

make a windows highlight search in c#


Would it be possible through c# to actually do a windows search (the one you find in Vista from the menu with higlighting (e.g you write 'fire' and get 'firefox')).

Thanks :)


Solution

  • Yes, this is possible with the Windows Desktop Search (WDS) API. You'll need the SDK, which even provides a .Net assembly if I recall correctly. Then look at the documentation to learn how to query the WDS index. It's quite simple, here's the C# example they provide:

    OleDbConnection conn = new OleDbConnection(
        "Data Source=(local);Initial Catalog=Search.CollatorDSO;Integrated Security=SSPI;User ID=<username>;Password=<password>");
    
    OleDbDataReader rdr = null;
    
    conn.Open();
    
    OleDbCommand cmd = new OleDbCommand("SELECT Top 5 System.ItemPathDisplay FROM SYSTEMINDEX", conn);
    
    rdr = cmd.ExecuteReader();
    
    while (rdr.Read())
    {
        Console.WriteLine(rdr[0]);
    }
    
    rdr.Close();
    conn.Close();
    

    When I used this in a project awhile back, the query string I used was built something like this:

    CSearchManager SearchManager = new CSearchManager();
    CSearchCatalogManager CatalogManager = SearchManager.GetCatalog("SystemIndex");
    CSearchQueryHelper QueryHelper = CatalogManager.GetQueryHelper();
    string connection_string = QueryHelper.ConnectionString;
    

    Then to do a simple file search:

    QueryHelper.QueryWhereRestrictions = "AND scope='file:'";
    QueryHelper.QuerySorting = "System.ItemNameDisplay ASC";
    string sqlQuery = QueryHelper.GenerateSQLFromUserQuery(Filename);
    

    From the documentation you can see how to build queries that get you the results you need.

    Now, a quick note. I was able to build a Vista Start Search clone, however, I did it by first scanning link files in the places where Vista stores Start Menu links (%appdata%\Microsoft\Windows\Start Menu & C:\ProgramData\Microsoft\Windows\Start Menu), then asynchronously loading WDS results in the background, which replicates Start Search behavior better than relying solely on WDS.