如何在资源管理器中选择文件 [英] How to get files selected in Explorer

查看:117
本文介绍了如何在资源管理器中选择文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编辑一个用vb.net语言在Visual Studio中编写的加载项.我需要的是一种从活动的Windows资源管理器窗口中获取所有当前选定文件的列表的方法,这样我就可以将其传递给程序中的另一个函数.我不是在Visual Studio方面经验丰富的人(我的大部分经验是在使用VB 6.0的VBA中进行的),所以在我花太多时间走错路之前,我正在寻求一些建议.

I have to edit an add-in that was written in visual studio in the vb.net language. What I need is a way to get a list of all the currently selected files from the active windows explorer window so that I can pass this to another function within the program. I'm not super experienced in visual studio (most of my experience has been in VBA which uses VB 6.0) so I'm looking for some advice before I spend too much time going down the wrong path.

我当时正在考虑使用Windows Shell对象.我已经找到了一些用C ++编写的示例,并且花了一些时间阅读MSDN,但是在为此花很多时间之前,我想在这里接触更多有经验的VB.Net/VS用户.我知道.Net有很多内置选项来处理system.io命名空间下的文件/文件夹对象,但是我还没有发现任何东西可以让我看到资源管理器窗口中当前选中的项目.

I was thinking of using the Windows Shell object. I've found some examples written in C++ and I've spent some time reading through the MSDN, but before I invest a ton of time in this I wanted to reach out here to more experienced VB.Net/VS users. I know .Net has a lot of built in options for dealing with file/folder objects under the system.io namespace, but I haven't found anything yet that would allow me to see what are the currently selected items in an explorer window.

我只是想知道.Net中是否有某些本机可以满足我的需求?
如果不是,使用Windows Shell对象是最好的方法吗?

I'm just wondering if there is something native within .Net that would do what I need?
If not, is using the Windows Shell object the best way to go?

推荐答案

这只是此答案的一个较小修订..而不是像链接的答案那样处理重点项目,而是从 ShellFolderView 获取选定的项目. System.IO 对您没有多大帮助,因为与File/Folder相关的类与文件系统有关-文件不知道是否选择了Explorer.

This is just a minor revision of this answer. Rather than work off the focused item as the linked answer does, get the selected items from the ShellFolderView. System.IO wont do you much good because the File/Folder related classes have to do with the file system - the files have no idea if Explorer has them selected.

首先,添加对 Microsoft Shell控件和自动化 Microsoft Internet控件的引用(请参见上面的链接).

First, add reference to Microsoft Shell Controls and Automation and Microsoft Internet Controls (see the above link).

Imports Shell32
Imports SHDocVw

Private Function GetExplorerSelectedFiles() As String()
    Dim ExplorerFiles As New List(Of String)

    Dim exShell As New Shell


    For Each window As ShellBrowserWindow In DirectCast(exShell.Windows, IShellWindows)
        ' check both for either interface to work
        '    add an Exit for to just work the first explorer window 
        If TryCast(window.Document, IShellFolderViewDual) IsNot Nothing Then
            For Each fi As FolderItem In DirectCast(window.Document, IShellFolderViewDual).SelectedItems
                ExplorerFiles.Add(fi.Path)
            Next

        ElseIf TryCast(window.Document, ShellFolderView) IsNot Nothing Then
            For Each fi As FolderItem In DirectCast(window.Document, ShellFolderView).SelectedItems
                ExplorerFiles.Add(fi.Path)
            Next

        End If
    Next

    Return ExplorerFiles.ToArray
End Function

用法(在按钮中单击):

Usage (in a button click):

Dim files = GetExplorerSelectedFiles()
lbFiles.Items.AddRange(files)

修改为可在 IShellFolderViewDual ShellFolderView

这篇关于如何在资源管理器中选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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