How to get Windows Explorer's selected files from within C#?

≡放荡痞女 提交于 2019-11-30 13:27:55
Daro

you don't need to get the Handle (of explorer).

In the project's references add these references found in the COM section. One needs to a reference to SHDocVw, which is the Microsoft Internet Controls COM object and Shell32, which is the Microsoft Shell Controls and Automation COM object.

Then add your:

using System.Collections;
using Shell32;
using System.IO;

Then this will work:

      string filename;  
      ArrayList selected = new ArrayList();
      foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindowsClass())
      {
        filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
        if (filename.ToLowerInvariant() == "explorer")
        {
          Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
          foreach (Shell32.FolderItem item in items)
          {
            selected.Add(item.Path);
          }
        }
      }
platon

The GetForegroundWindow is a Win32 API function and to use it, you need to import it as explained here: getforegroundwindow (user32)

Shell32 is described here:

working with shell 32 in C#

Finally, I do not know your task, but usually if it is necessary to select some files and get access to this collection, it is necessary to use the FileOpenDialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!