从 Windows 桌面获取所选文件的列表 [英] Get list of selected files from Windows Desktop
问题描述
我正在尝试从 Windows 桌面和资源管理器 Windows 获取所选文件的列表.要求是我应该能够从活动的资源管理器窗口或桌面检索当前选择.
I am trying to get a list of selected files from the Windows Desktop and the Explorer Windows. The requirement is that I should be able to retrieve the current selection from the active explorer window or the Desktop.
在浏览了在线资源后,我设法将以下代码组合在一起,但它没有提供桌面中所选项目的列表.
I have managed to put together the following code, after going through online resources, but it does not provide a list of selected items from the Desktop.
ArrayList selected = new ArrayList();
var shell = new Shell32.Shell();
IntPtr handle = IntPtr.Zero;
handle = GetForegroundWindow();
int intHandle = handle.ToInt32();
//For each explorer
foreach (InternetExplorer window in new ShellWindowsClass())
{
if (window.HWND == (int)handle)
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
selected.Add(item.Path);
}
}
}
除此之外,我尝试了以下操作,但它只提供了所有打开的资源管理器窗口中所有选定元素的列表,而忽略了桌面.
Other than that, I tried the following but it just gives a list of all selected elements in all open explorer windows while ignoring the Desktop.
string filename; = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
if (filename.ToLowerInvariant() == "explorer")
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
//MessageBox.Show(item.Path.ToString());
selected.Add(item.Path);
}
}
所以我总是以资源管理器窗口中的列表结束,即使没有打开资源管理器窗口也没有得到任何结果.当前的技术似乎完全忽略了桌面.
So I just always end up with a list from the explorer windows and get no results even when no explorer windows are open. The current techniques seem to be ignoring the Desktop altogether.
如果有人能帮我从当前活动的窗口/桌面中获取所选文件的列表,我将不胜感激.
I would really appreciate it if someone could help me out to get a list of selected files from the currently active window/desktop.
谢谢.
推荐答案
桌面很容易,因为它仍然是一个列表视图,只需找到正确的句柄.列表视图是桌面句柄的子视图.
It is easy for desktop since it is still a listview, just find the correct handle. list view is a child of the desktop handle.
Desktop
+- Progman (for backward compatibility)
+- Shell Def View
+- SysListView32 (even under 64 bit)
那么你就可以在列表视图上做所有的列表视图操作了.但其他资源管理器窗口不包含列表视图.相反,他们使用带有 DirectUIHWND
类的窗口,这对许多人来说是个谜.我刚刚找到了一篇文章,描述了解开这个谜团的方法.
then you can do all listview operations on the list view. but other explorer windows does not contain a list view. Instead they use window with class DirectUIHWND
which is a mystery to many. I've just found a post that describes a way to the unravel that mystery.
http://smartbear.com/forums?forumid=81&threadid=68427#68428
希望能帮到你.
这篇关于从 Windows 桌面获取所选文件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!