Windows search - full text search in c#
Asked Answered
P

2

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.

Pyrometallurgy answered 30/6, 2013 at 10:27 Comment(2)
What do you mean by FullText ?Rationalize
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.Barty
P
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();
Pyrometallurgy answered 6/7, 2013 at 22:59 Comment(6)
Works on Windows 8.1 in desktop modeLupien
This doesn't work for me if u want to find part of a word. For example: if the document contained 'dummy', and I used the code above to search for 'dum'. Any idea how to use wild cards here? Just adding a * doesn't seem to work.Lupien
@Lupien did you tried * for search ? not solutionConvenient
Scope is file ? not folder ?Convenient
The syntax is above. You use keyword file and set folder there. Why I don't know, I haven't created this API.Barty
Works for window 10!Banderole
P
1

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

That's what you want.

Piccoloist answered 10/7, 2013 at 10:3 Comment(3)
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.Barty
MS sample download link offline :)Telugu
That link missing Microsoft.Search.Interop.dll, I am searh for the dll now.Banderole

© 2022 - 2024 — McMap. All rights reserved.