19

I am looking for a code that gets results of full text search using Windows search (it should be available in Vista, 7 and 8 by default).

I have found some questions here and some texts on msdn, but none of them have some exact code that works. I have tried with Windows API Code Pack (as it is mentioned as one of the interfaces to Windows Search), but it returns results only for file names, not for full text.

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • What do you mean by `FullText` ? – Shaharyar Jun 30 '13 at 10:29
  • 1
    Full text means that Windows search will search the text (content) of document files (like .txt, .doc, .docx, .pdf), not just a file name or its properties. If you search with Windows Explorer, it is possible, so it should be possible. – Ivan Ičin Jun 30 '13 at 12:44

2 Answers2

29

Here is the code that does work - in example I made it to search for the word "dummy" in the desktop folder:

string connectionString = "Provider=Search.CollatorDSO;Extended Properties=\"Application=Windows\"";
OleDbConnection connection = new OleDbConnection(connectionString);

string query = @"SELECT System.ItemName FROM SystemIndex " +
   @"WHERE scope ='file:" + System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "' and FREETEXT('dummy')";
OleDbCommand command = new OleDbCommand(query, connection);
connection.Open();

List<string> result = new List<string>();

OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    result.Add(reader.GetString(0));
}

connection.Close();
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
1

Take a look at the DSearch example. Windows Search Code Samples

That's what you want.

Trogvar
  • 856
  • 6
  • 17
user743414
  • 936
  • 10
  • 23
  • Thank you, I have already found the solution. That was one of the pages I needed, but it was far from enough (that sql example doesn't explain available sql syntax for Windows Search, which was necessary for me and the problem I have mentioned). You have the complete code that works in my answer above. – Ivan Ičin Jul 10 '13 at 19:06
  • 2
    MS sample download link offline :) – zozzancs Jan 19 '16 at 16:06
  • That link missing Microsoft.Search.Interop.dll, I am searh for the dll now. – Tony Dong Jun 06 '19 at 22:26