【问题标题】:Get current selection in WindowsExplorer from a C# application?从 C# 应用程序获取 WindowsExplorer 中的当前选择?
【发布时间】:2012-01-07 17:47:48
【问题描述】:

是否可以从我的 C# 应用程序中获取当前在 Windows 资源管理器中选择的文件列表?

我对通过 C# 等托管语言与 Windows 资源管理器进行交互的不同方法进行了大量研究。最初,我正在研究 shell 扩展的实现(例如 herehere),但显然在托管代码中这是一个坏主意,而且对于我的情况来说可能是矫枉过正。

接下来,我查看了 PInvoke/COM 解决方案,发现了 this article,这导致我找到了这段代码:

        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 对象没有获取当前文件选择的方法,尽管它们可用于获取有关窗口的信息。

然后我发现this article 完全符合我的要求,但使用的是 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 对象,但我仍然看不到获取当前选择的方法。

我可能完全看错了地方,但我一直在跟踪我仅有的线索。谁能给我指出合适的 PInvoke/COM 解决方案?

【问题讨论】:

    标签: c# .net windows com pinvoke


    【解决方案1】:

    终于找到解决办法了,感谢这个问题:Get selected items of folder with 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 对应于资源管理器窗口中的实际文件夹视图,这不是很直观。但除了具有误导性的变量/方法名称之外,这非常有效。

    【讨论】:

    • GetForegroundWindow() 方法中包含什么?
    • 其实我应该问一下,如何实现 PInvoke 以便可以使用 GetForegroundWindow 方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2013-04-09
    • 2011-01-06
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    相关资源
    最近更新 更多