获取当前选择WindowsExplorer从C#应用程序? [英] Get current selection in WindowsExplorer from a C# application?

查看:264
本文介绍了获取当前选择WindowsExplorer从C#应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能得到,目前在Windows资源管理器中选择从我的C#应用​​程序的文件列表?

Is it possible to get a list of the files that are currently selected in Windows Explorer from my C# app?

我已经与Windows资源管理器,如C#托管语言交互的不同方法进行了大量的研究。起初,我在这里看着外壳扩展的实现(和的here 为例),但显然这是从内部管理code一个坏主意,可能是矫枉过正反正我的情况。

I have done a lot of research on different methods of interacting with Windows Explorer from a managed language like C#. Initially, I was looking at implementations of shell extensions (here and here for example), but apparently that is a bad idea from within managed code, and is probably overkill for my situation anyway.

接下来,我看着的PInvoke / COM解决方案,并发现这篇文章,这使我这个code:

Next, I looked into PInvoke/COM solutions, and found this article, which led me to this code:

        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 对象有没有方法来获得当前的文件选择,虽然他们可以用来获取有关窗口的信息。

...But the individual InternetExplorer objects have no methods to get the current file selection, though they can be used to get information about the window.

然后我发现这篇文章做正是我需要的,但在C ++。以此为出发点,我试图通过添加 SHELL32.DLL 在我的项目的引用,做一些翻译。我结束了以下内容:

Then I found this article doing exactly what I needed, but in C++. Using this as a starting point, I attempted to do some translation by adding shell32.dll as a reference in my project. I ended up with the following:

        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)
                {
                    ...
                }
            }
        }

这是稍微靠近,因为我能够得到的窗口文件夹对象,并为每个项目,但我还是不明白的方式来获得当前的选择。

This was slightly closer, because I am able to get a Folder object for the window, and for each item, but I still do not see a way to get the current selection.

我可能会寻找完全错了地方,但我一直在关注的唯一线索我。任何人都可以点我到适当的PInvoke / COM解决方案?

I may be looking entirely in the wrong place, but I've been following the only leads I have. Can anyone point me to the appropriate PInvoke/COM solution?

推荐答案

终于想出了一个解决方案,这要归功于这个问题:的获取选定文件夹中的项目与WinAPI的

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

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

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.

这篇关于获取当前选择WindowsExplorer从C#应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆