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 inter
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.