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?