Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of selected files from Windows Desktop

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.

like image 940
Shumais Ul Haq Avatar asked Sep 30 '13 13:09

Shumais Ul Haq


1 Answers

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.

Desktop
+- Progman (for backward compatibility)
   +- Shell Def View
      +- SysListView32 (even under 64 bit)

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.

like image 120
Erdogan Kurtur Avatar answered Oct 26 '22 14:10

Erdogan Kurtur