从C#应用程序获取WindowsExplorer中的当前select?

是否有可能从我的C#应用​​程序获取当前在Windows资源pipe理器中select的文件的列表?

我已经做了很多关于从C#这样的托pipe语言与Windows资源pipe理器交互的不同方法的研究。 最初,我正在研究shell扩展的实现( 在这里和这里例如),但显然这是从托pipe代码中的一个坏主意,可能无论如何对我的情况可能矫枉过正。

接下来,我看着PInvoke / COM解决scheme,并find这篇文章 ,这导致我这样的代码:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); string filename; ArrayList windows = new ArrayList(); foreach(SHDocVw.InternetExplorer ie in shellWindows) { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if(filename.Equals("explorer")) { Console.WriteLine("Hard Drive: {0}", ie.LocationURL); windows.Add(ie); var shell = new Shell32.Shell(); foreach (SHDocVw.InternetExplorerMedium sw in shell.Windows()) { Console.WriteLine(sw.LocationURL); } } } 

…但个别的InternetExplorer对象没有获得当前文件select的方法,虽然它们可以用来获取有关窗口的信息。

然后我发现这篇文章正是我所需要的,但在C ++中。 以此为出发点,我试图通过在我的项目中添加shell32.dll作为参考进行一些翻译。 我结束了以下几点:

  SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows(); string filename; ArrayList windows = new ArrayList(); foreach(SHDocVw.InternetExplorer ie in shellWindows) { filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); if(filename.Equals("explorer")) { Console.WriteLine("Hard Drive: {0}", ie.LocationURL); windows.Add(ie); var shell = (Shell32.IShellDispatch4)new Shell32.Shell(); Shell32.Folder folder = shell.NameSpace(ie.LocationURL); Shell32.FolderItems items = folder.Items(); foreach (Shell32.FolderItem item in items) { ... } } } 

这是稍微接近,因为我能够得到一个Folder对象的窗口,并为每个项目,但我仍然没有看到一种方式来获取当前的select。

我可能完全看错了地方,但我一直在追踪我唯一的线索。 任何人都可以指点我适当的PInvoke / COM解决scheme吗?

最后想出了一个解决方案,感谢这个问题: 使用WinAPI获取文件夹的选定项目 。

我结束了以下,为了得到当前选定的文件列表:

 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); } } } 

显然window.Document对应于资源管理器窗口内的实际文件夹视图,这不是很直观。 但是,除了误导性的变量/方法名称,这完美的作品。