C#如何获取Windows资源管理器选中的文件?我需要获取在Windows资源管理器中选择的当前文件集合。我从这里找到了以下代码。然而,我不在。首先,GetForegroundWindow从何而来?另一方面,编译器在varshell=newShell32.Shell();行抱怨说“找不到类型或名称空间名称‘Shell32’(您是否缺少using指令或程序集引用?)”。我添加了SHDocVw作为参考,但我仍然无法通过编译器。有人可以帮我完成这个吗?IntPtrhandle=GetForegroundWindow();ArrayListselected=newArrayList();varshell=newShell32.Shell();foreach(SHDocVw.InternetExplorerwindowinshell.Windows()){if(window.HWND==(int)handle){Shell32.FolderItemsitems=((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();foreach(Shell32.FolderItemiteminitems){selected.Add(item.Path);您不需要获取Handle(Explorer)。在项目引用的COM部分添加这些引用。需要引用SHDocVw(MicrosoftInternetControlsCOM对象)和Shell32(MicrosoftShellControlandAutomationCOM对象)。然后添加您的:使用System.Collections;使用Shell32;使用System.IO;然后这将起作用:字符串文件名;ArrayListselected=newArrayList();foreach(SHDocVw.InternetExplorerwindowinnewSHDocVw.ShellWindowsClass()){filename=Path.GetFileNameWithoutExtension(window.FullName).ToLower();if(filename.ToLowerInvariant()=="explorer"){Shell32.FolderItemsitems=((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();foreach(项目中的Shell32.FolderItem项目){selected.Add(item.Path);}}}GetForegroundWindow是一个Win32API函数,要使用它,您需要按照此处的说明导入它:getforegroundwindow(user32)Shell32的说明如下:UsingtheshellinC#32最后,我不知道您的任务,但通常如果需要选择一些文件并访问这个集合,你必须使用FileOpenDialog选择文件?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
