Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

winforms - How to use Windows Search Service in c#

I'm working on an application, an user can search for files or folders either on the local computer or on the network. I am using DirectoryInfo.GetDirecotories().

  1. But I also want to add the functionality that windows 7 uses for searching, I believe that uses indexing. I also saw Windows Searching Service on msdn, but I'm not sure which way is best: querying the indexed catalog or using the search service. Any suggestions?
  2. I was wondering if anyone can give me a small example in C# that searches the indexed catalog.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the below example:

static void Main(string[] args)
{
    var connection = new OleDbConnection(@"Provider=Search.CollatorDSO;Extended Properties=""Application=Windows""");

    // File name search (case insensitive), also searches sub directories
    var query1 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE scope ='file:C:/' AND System.ItemName LIKE '%Test%'";

    // File name search (case insensitive), does not search sub directories
    var query2 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE directory = 'file:C:/' AND System.ItemName LIKE '%Test%' ";

    // Folder name search (case insensitive)
    var query3 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE scope = 'file:C:/' AND System.ItemType = 'Directory' AND System.Itemname LIKE '%Test%' ";

    // Folder name search (case insensitive), does not search sub directories
    var query4 = @"SELECT System.ItemName FROM SystemIndex " +
                @"WHERE directory = 'file:C:/' AND System.ItemType = 'Directory' AND System.Itemname LIKE '%Test%' ";           

    connection.Open();

    var command = new OleDbCommand(query4, connection);

    using (var r = command.ExecuteReader())
    {
        while (r.Read())
        {
            Console.WriteLine(r[0]);
        }
    }

    connection.Close();

    Console.ReadKey();
}

It's using OLE DB api to connect to the indexer service and use a SQL-like syntax to search System objects in its SystemIndex table. You have 4 example queries which do different things. All example queries will search in the c: folder for items that contain Test in their names.

You can search for files, folders mails and possibly other media (depending on OS) on local or other machines. From what I have researched network drives are not supported since they can't be indexed but you can connect to other machines for which I assume RPC is used in the background which means you have to supply network credentials using a different api (e.g. System.Net).

Note that for any of this to work your indexing must be fully operational on the target machine (which it is by default). The api is corresponding to whatever you specify in your Indexing Options. This is the screen in question:

indexing options

The full list of properties for the System object can be found here: Property System Reference. This object contains things such as URL, Path, Name, Date etc.

More interesting examples using different predicates (e.g. scope and directory) can be found here: Windows Vista Search Syntax. There is also a crude MSDN documentation: SCOPE and DIRECTORY Predicates

I recommend you check out the documentation because you can do a lot of stuff with this api.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...