Get current selection in WindowsExplorer from a C# application?

前端 未结 1 658
悲&欢浪女
悲&欢浪女 2020-12-06 12:29

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

相关标签:
1条回答
  • 2020-12-06 13:23

    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.

    0 讨论(0)
提交回复
热议问题