Restricted FolderBrowserDialog, recommended soluti

2020-07-25 09:32发布

问题:

We want to prevent the user from doing anything except selecting a folder. We don't want to allow him to delete files/folders, rename them, access the context menu, etc. But we can't override anything since FolderBrowserDialog is sealed.

We googled around and found some solutions:

  • Implement our own FolderBrowserDialog: Don't have time for this, only acceptable as last resort
  • This guy did it for an OpenFileDialog, might work but seems a little overkill

Anyone faced this problem and found an optimal solution for this? It must be .NET 4.0 compatible

回答1:

You best bet is to use a control like FolderView from Shell MegaPack - it is customizable in the way you want it - you can turn of shell context menus, renaming, drag-drop, keyboard shortcuts, etc,

DISCLAIMER: We are the developers of FolderView mentioned above.



回答2:

In the end, we solved it implementing our own control. It was a lot simpler than we thought. In short, we just made a class inherit from System.Windows.Forms.Form, with a System.Windows.Forms.TreeView on the left and a System.Windows.Forms.ImageList on the right to show the current folder's contents. Then we DllImport'ed SHGetFileInfo and ExtractIconEx from Shell32 in order to make the TreeView and ImageList's icons look like Explorer's, and voila!