
我已经对从C#等托管语言与windows资源管理器交互的不同方法做了大量研究.最初,我正在研究shell扩展的实现(例如here和here),但显然这是托管代码中的一个坏主意,并且无论如何可能对我的情况有点过分.
接下来,我查看了PInvoke / COM解决方案,找到了this article,这使我得到了这段代码:
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对象没有获取当前文件选择的方法,尽管它们可用于获取有关窗口的信息.
然后我发现this article正是我所需要的,但在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解决方案?
解决方法 最后找到了一个解决方案,感谢这个问题: Get selected items of folder with 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对应于资源管理器窗口中的实际文件夹视图,这不是很直观.但除了误导性的变量/方法名称之外,这非常有效.
总结以上是内存溢出为你收集整理的从C#应用程序获取WindowsExplorer中的当前选择?全部内容,希望文章能够帮你解决从C#应用程序获取WindowsExplorer中的当前选择?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)