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

发布于 2024-12-18 11:25:26 字数 2480 浏览 0 评论 0原文

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

我对通过 C# 等托管语言与 Windows 资源管理器交互的不同方法进行了大量研究。最初,我正在研究 shell 扩展的实现(此处此处为例),但显然这是一个托管代码中的想法很糟糕,而且对于我的情况来说可能有点过分了。

接下来,我研究了 PInvoke/COM 解决方案,并找到了这篇文章,这让我找到了这段代码:

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

然后我发现这篇文章所做的正是我需要,但是是用 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 解决方案?

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 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.

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

            }
        }

...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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2024-12-25 11:25:26

终于找到了一个解决方案,感谢这个问题: 获取文件夹的选定项目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 对应于资源管理器窗口内的实际文件夹视图,这不是很直观。但除了误导性的变量/方法名称之外,这一切都很完美。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文