Programmatically sort folders on Windows 8

和自甴很熟 提交于 2020-01-03 15:37:10

问题


Windows 8 does not seem to persist sort orders on individual folders. I'm guessing when explorer.exe terminates (like when rebooting) these preferences get discarded? Beats me, all I know is after a time (like a few days) those preferences get totally lost.

Is there a way to duplicate programmatically sorting folders in Windows Explorer (opting one of the 'Sort by' options in the context menu)?

Such that, say I have a folder opened in the Windows UI. Say the folder is currently set to sort by 'Name' and after running the program which will sort it by size I check the sort order in Windows Explorer again and it now says 'Size'?

I have a certain big folder structure that I want all of it and its subfolders sorted by size permanently. So I would like to run this program before viewing them and not have to individually manually re-sort them all first. Thanks!


回答1:


The sort order belongs to the view, not folder. You can have two Windows Explorer windows open on the same folder, each has a different sort order.

To force a sort order when Windows Explorer navigates to a specific folder, first hook the DShellWindowsEvents::WindowRegistered event on the shellwindows object, then each time when the WindowRegistered event triggers, enumerate through the shell window list and compare with your existing enumeration to find the new windows explorer instance. Once you get hand on the new instance, hook up the DWebBrowserEvents2::DocumentComplete event for that instance to listen to its navigations.

When a navigation is complete, the DocumentComplete event will give you the target URL which you can use to detect if the target is inside your big folder structure. If so, query the IShellBrowser service from the windows explorer instance, then call QueryActiveShellView to get the shell view. Once you get the shell view, QI for IFolderView2 and then call SetSortColumns.

The Automate the Active Windows Explorer or Internet Explorer Window sample on codeproject has most of the code, except that it calls IShellView::SelectItem to simulate SHOpenFolderAndSelectItems at the end.




回答2:


You can save Explorer sort settings by holding CTRL while clicking on that X on top right.

http://blog.chron.com/helpline/2009/01/saving-explorer-sort-order/



来源:https://stackoverflow.com/questions/31259085/programmatically-sort-folders-on-windows-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!