make a windows highlight search in c#

僤鯓⒐⒋嵵緔 提交于 2019-12-01 10:39:22

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.

Windows search uses an index to achieve the results by querying the index as the text in the search field is updated. In order for this to work the engine must be capable of returning results very quickly so a collection which is very efficient for lookups is a good idea.

You would then query the hashtable when the text in the search box changes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!