Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current selection in WindowsExplorer from a C# application?

Is it possible to get a list of the files that are currently selected in Windows Explorer from my C# app?

I have done a lot of research on different methods of interacting with Windows Explorer from a managed language like C#. Initially, I was looking at implementations of shell extensions (here and here for example), but apparently that is a bad idea from within managed code, and is probably overkill for my situation anyway.

Next, I looked into PInvoke/COM solutions, and found this article, which led me to this code:

        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

        string filename;
        ArrayList windows = new ArrayList();

        foreach(SHDocVw.InternetExplorer ie in shellWindows)
        {
            filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
            if(filename.Equals("explorer"))
            {
                Console.WriteLine("Hard Drive: {0}", ie.LocationURL);
                windows.Add(ie);

                var shell = new Shell32.Shell();
                foreach (SHDocVw.InternetExplorerMedium sw in shell.Windows())
                {
                    Console.WriteLine(sw.LocationURL);
                }

            }
        }

...But the individual InternetExplorer objects have no methods to get the current file selection, though they can be used to get information about the window.

Then I found this article doing exactly what I needed, but in C++. Using this as a starting point, I attempted to do some translation by adding shell32.dll as a reference in my project. I ended up with the following:

        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

        string filename;
        ArrayList windows = new ArrayList();

        foreach(SHDocVw.InternetExplorer ie in shellWindows)
        {
            filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
            if(filename.Equals("explorer"))
            {
                Console.WriteLine("Hard Drive: {0}", ie.LocationURL);
                windows.Add(ie);
                var shell = (Shell32.IShellDispatch4)new Shell32.Shell();
                Shell32.Folder folder = shell.NameSpace(ie.LocationURL);
                Shell32.FolderItems items = folder.Items();
                foreach (Shell32.FolderItem item in items)
                {
                    ...
                }
            }
        }

This was slightly closer, because I am able to get a Folder object for the window, and for each item, but I still do not see a way to get the current selection.

I may be looking entirely in the wrong place, but I've been following the only leads I have. Can anyone point me to the appropriate PInvoke/COM solution?

like image 387
WilHall Avatar asked Nov 28 '11 08:11

WilHall


1 Answers

Finally figured out a solution, thanks to this question: Get selected items of folder with WinAPI.

I ended up with the following, in order to get a list of currently selected files:

IntPtr handle = GetForegroundWindow();

List<string> selected = new List<string>();
var shell = new Shell32.Shell();
foreach(SHDocVw.InternetExplorer window in shell.Windows())
{
    if (window.HWND == (int)handle)
    {
        Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
        foreach(Shell32.FolderItem item in items)
        {
            selected.Add(item.Path);
        }
    }
}

Apparently window.Document corresponds to the actual folder view inside the explorer window, which isn't very intuitive. But other than the misleading variable/method names, this works perfectly.

like image 108
WilHall Avatar answered Sep 25 '22 14:09

WilHall