Display a Windows Search result in Windows Explorer
Asked Answered
A

2

5

Using the Windows API Code Pack I wrote this code here (in C#), using the Windows Search.

How to display this result in Windows Explorer (where it should be displayed)?

// create the leaf condition for the file name
        SearchCondition fileNameCondition =
            SearchConditionFactory.CreateLeafCondition(
            SystemProperties.System.FileName, textBox2.Text, 
            SearchConditionOperation.Equal);

        // create the search folder
        ShellSearchFolder searchFolder = new ShellSearchFolder(fileNameCondition(ShellContainer)NonFileSystemKnownFolder.FromParsingName(@>"C:\Users\ILIANHOME\Downloads"));    

Simple right? Not for me, I'm a pretty novice programmer, thanks in advance for any help:)

Ame answered 31/3, 2013 at 21:55 Comment(0)
N
6

Unfortunately this doesn't seem to be a need that the Windows API Code Pack was designed to meet. That library is all about taking shell concepts and bringing them into managed code. At the API level, it is technically independent of the "real" Windows shell, such that it could be implemented using a different data provider than the actual COM shell APIs. With that goal in mind, the ability to "go back" to the native shell is difficult, as there may not be a native shell to go back to (speaking hypothetically of course; I don't know of anyone making an alternative implementation). In this regard, the library seems to think of itself as an extension of the Framework Class Library (which is probably valid, since some of the features -- like JumpList -- did eventually make it into the core libraries).

Note that I can't speak for the authors of the library, the above is purely speculation based on the structure of the library and my experience with other .NET libraries from Microsoft. But whatever the reason, this functionality doesn't seem to exist.

What does exist is the ability to create your own Explorer window, through the ExplorerBrowser control (or the WPF wrapper of it). See the ExplorerBrowser sample that comes with the library for an example of that. I can't say that I recommend trying to imitate Explorer, though, even with these helpers.

For your particular problem of launching the search window, I would recommend looking into the search: protocol and see if it meets your needs. It doesn't have a nice object model to represent the queries, so you'd have to either make one yourself (or find one, it may exist) or just work with strings. But it is very flexible.

Your particular problem as above could be implemented as:

string folder = Uri.EscapeDataString(@"C:\Users\ILIANHOME\Downloads");
string file = '"' + Uri.EscapeDataString(textBox2.Text) + '"';
string uri = "search:query=filename:" + file + "&crumb=location:" + folder;
Process.Start(new ProcessStartInfo(uri));
Naturalism answered 1/4, 2013 at 4:21 Comment(1)
That's really awesome...for a single file. How can I "force" the Windows Search to give me specific files from different folders...using the search protocol ? Could you please give me an example ? Thanks.Ame
M
3
  public void getPDF()
    {
                string folder = Uri.EscapeDataString(@"C:\");
                string file = "*.pdf* size:> 2 MB";
                string uri = "search:query=" + file + "&crumb=location:" + folder;
                Process.Start(new ProcessStartInfo(uri));
    }
Mcgray answered 25/4, 2016 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.