问题描述
我想拉出来在一个开放的资源管理器窗口中列出的文件和目录的列表(在为它们显示的顺序相同),这样我可以去翻它,然后将焦点设置到一个特定的项目。
I'm trying to pull out the list of files and directories listed in an open Explorer window (in the same order as they're displayed) so that I can look through it, then set focus to a particular item.
我发现这个代码的,让我得到的选的项目,但我不知道是否有可能使用这种方法来获得的所有的项目:
I found this code here that allows me to get the selected items, however I'm not sure if it's possible to use this approach to get all items:
List<string> SelectedFiles() {
string filename;
List<string> selected = new List<string>();
var shell = new Shell32.Shell();
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows()) {
filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer") {
((Shell32.IShellFolderViewDual2)window.Document).SelectItem()
foreach (Shell32.FolderItem item in items) {
selected.Add(item.Path);
}
}
}
return selected;
}
它看起来像这样SHELL32方法也可以让我以编程方式选择一个项目,这就是我试图完成其他部分。取而代之的 SelectedItems()
,我称之为的SelectItem()
,虽然我不知道如何使用该功能
It looks like this Shell32 approach would also allow me to select an item programmatically, which is the other part I'm trying to accomplish. Instead of SelectedItems()
, I would call SelectItem()
, though I'm not sure how to use that function.
任何人都知道的一种方式来获得的文件/目录从打开Windows资源管理器窗口的列表(理想情况下将焦点设置到项目)?也许的P / Invoke样的事情?
Anyone know of a way to get the list of files/directories from an open Windows Explorer window (and ideally set focus to an item)? Perhaps a P/Invoke kind of thing?
推荐答案
我能够修改代码片段,我发现列出所有文件/目录,而不是只选择那些
I was able to modify that code snippet I found to list all files/directories instead of just the selected ones.
下面是我结束了:
List<string> FilesAndFolders() {
string filename;
List<string> explorerItems = new List<string>();
var shell = new Shell32.Shell();
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows()) {
filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer") {
Shell32.Folder folder = ((Shell32.IShellFolderViewDual2)window.Document).Folder;
Shell32.FolderItems items = folder.Items();
foreach (Shell32.FolderItem item in items) {
explorerItems.Add(item.Path);
}
}
}
return explorerItems;
}
修改
要选择一个项目,你可以调用:
To select an item, you call:
((Shell32.IShellFolderViewDual2)window.Document).SelectItem(项目,1);
其中,窗口
是 SHDocVw.InternetExplorer
和项目
是 Shell32.FolderItem
(从 folder.Items()
在上面的例子)。
要取消选择,它通过在 0
而不是 1
作为第二个重载。
where window
is a SHDocVw.InternetExplorer
, and item
is a Shell32.FolderItem
(from folder.Items()
in the above example).To deselect, it, pass in 0
instead of 1
as the second overload.
这篇关于获取在C#中打开资源管理器窗口的文件/目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!