I am trying to get a list of selected files from the Windows Desktop and the Explorer Windows. The requirement is that I should be able to retrieve the current selection from the active explorer window or the Desktop.
I have managed to put together the following code, after going through online resources, but it does not provide a list of selected items from the Desktop.
ArrayList selected = new ArrayList();
var shell = new Shell32.Shell();
IntPtr handle = IntPtr.Zero;
handle = GetForegroundWindow();
int intHandle = handle.ToInt32();
//For each explorer
foreach (InternetExplorer window in new ShellWindowsClass())
{
if (window.HWND == (int)handle)
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
selected.Add(item.Path);
}
}
}
Other than that, I tried the following but it just gives a list of all selected elements in all open explorer windows while ignoring the Desktop.
string filename; = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer")
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
//MessageBox.Show(item.Path.ToString());
selected.Add(item.Path);
}
}
So I just always end up with a list from the explorer windows and get no results even when no explorer windows are open. The current techniques seem to be ignoring the Desktop altogether.
I would really appreciate it if someone could help me out to get a list of selected files from the currently active window/desktop.
Thank You.
It is easy for desktop since it is still a listview, just find the correct handle. list view is a child of the desktop handle.
then you can do all listview operations on the list view. but other explorer windows does not contain a list view. Instead they use window with class
DirectUIHWND
which is a mystery to many. I've just found a post that describes a way to the unravel that mystery.http://smartbear.com/forums?forumid=81&threadid=68427#68428
I hope it helps.
I think you should communicate between processes. Following topics will help.
This is an example of retrieving icons from desktop. List of desktop items and their current positions are fetched. http://social.msdn.microsoft.com/Forums/windows/en-US/d7df8a4d-fc0f-4b62-80c9-7768756456e6/how-can-i-get-desktops-icons-information-?forum=winforms
Here the parameter LVM_GETITEMSTATE can be used within the sample code from the link above. http://msdn.microsoft.com/en-us/library/windows/desktop/bb761053(v=vs.85).aspx
Good luck..