j6t
j6t

Reputation: 13377

How to launch Windows Explorer with a custom Search Result

I am writing a Windows application, which collects (possibly hundreds of) names of files, all of which live in subfolders of one particular folder (which the user can select). The list is just a subset of all the files in the folders.

I do not want to implement a user-interface that offers all kinds of sorting and selection possibilities because Windows Explorer is always much better in this regard.

Is it there an API that allows me to launch Windows Explorer from my application such that it displays my list of files as if it were the result of a search operation?

Upvotes: 0

Views: 465

Answers (2)

Anders
Anders

Reputation: 101569

The Explorer saved search format (.search-ms) is documented on MSDN. The only downside is that it will perform the actual search when you open it, it does not contain a list of paths of files found.

If this is unacceptable then you will have to get your hands dirty deep in IShellFolder and friends.

If hosting your own window is acceptable then IExplorerBrowser will get you 99% of the way there. Call IExplorerBrowser::FillFromObject to fill the view with a custom list of files or manipulate the view directly. Example code here.

If you must display the list in Explorer then I think you will have to bite the bullet and implement a namespace extension.

Upvotes: 2

Strive Sun
Strive Sun

Reputation: 6289

You can use the SHOpenFolderAndSelectItems function to open one particular folder.

LPCWSTR pszPathToOpen = L"C:\\Users\\strives";
PIDLIST_ABSOLUTE pidl;
if (SUCCEEDED(SHParseDisplayName(pszPathToOpen, 0, &pidl, 0, 0)))
{
    // we don't want to actually select anything in the folder, so we pass an empty
    // PIDL in the array. if you want to select one or more items in the opened
    // folder you'd need to build the PIDL array appropriately
    ITEMIDLIST idNull = { 0 };
    LPCITEMIDLIST pidlNull[1] = { &idNull };
    SHOpenFolderAndSelectItems(pidl, 1, pidlNull, 0);
    ILFree(pidl);
}

Alternatively, you can call ShellExecute on the folder directly to run its default action (which is normally to open in a browser window):

ShellExecute(NULL, NULL, "C:\\Users\\strives", NULL, NULL, SW_SHOWNORMAL);

Upvotes: 0

Related Questions