Get selected items of folder with WinAPI

Solution 1:

That's a lot of hacking to do something that is explicitly supported by the various shell objects and interfaces. Granted the documentation doesn't make it easily discoverable, but the functionality is there. Raymond Chen wrote a great article about using these interfaces. There doesn't appear to be a way to get the "current" folder, though I guess you could get the HWNDs and see if any is the foreground window.

Solution 2:

thank you very much. You gave me the right direction. It is possible to get the selected files of a folder:

/// <summary>
/// Get the selected file of the active window
/// </summary>
/// <param name="handle">Handle of active window</param>
/// <returns></returns>
public String getSelectedFileOfActiveWindow(Int32 handle)
{
    try
    {
        // Required ref: SHDocVw (Microsoft Internet Controls COM Object)
        ShellWindows shellWindows = new SHDocVw.ShellWindows();

        foreach (InternetExplorer window in shellWindows)
        {
            if (window.HWND == handle)
                return ((Shell32.IShellFolderViewDual2)window.Document).FocusedItem.Path;
        }                
    }
    catch (Exception)
    {
        return null;
    }
    return null;
}