2017-11-27 13:11:20 -05:00
using System ;
using System.ComponentModel ;
using System.Drawing ;
using System.Runtime.InteropServices ;
using System.Threading ;
using System.Windows.Forms ;
using Microsoft.Win32 ;
using Vanara.Extensions ;
using Vanara.InteropServices ;
using Vanara.PInvoke ;
using Vanara.Resources ;
using static Vanara . PInvoke . ComCtl32 ;
using static Vanara . PInvoke . Kernel32 ;
using static Vanara . PInvoke . Shell32 ;
2017-12-13 12:20:03 -05:00
using static Vanara . PInvoke . User32_Gdi ;
2017-11-27 13:11:20 -05:00
namespace Vanara.Windows.Forms
{
/// <summary>
/// </summary>
public enum FolderBrowserDialogOptions
{
/// <summary></summary>
Folders ,
/// <summary></summary>
FoldersAndFiles ,
/// <summary></summary>
Computers ,
/// <summary></summary>
Printers
}
/// <summary>
/// Standard folders registered with the system as Known Folders. A computer will have only folders appropriate to it installed.
/// </summary>
2018-03-27 19:04:44 -04:00
public enum KnownFolder
2017-11-27 13:11:20 -05:00
{
/// <summary>Account Pictures</summary>
2018-03-27 19:04:44 -04:00
AccountPictures = KNOWNFOLDERID . FOLDERID_AccountPictures ,
2017-11-27 13:11:20 -05:00
/// <summary>Get Programs</summary>
2018-03-27 19:04:44 -04:00
AddNewPrograms = KNOWNFOLDERID . FOLDERID_AddNewPrograms ,
2017-11-27 13:11:20 -05:00
2018-03-27 19:04:44 -04:00
/// <summary>Admin tools</summary>
AdminTools = KNOWNFOLDERID . FOLDERID_AdminTools ,
2017-11-27 13:11:20 -05:00
2018-03-27 19:04:44 -04:00
/// <summary>Application shortcuts</summary>
ApplicationShortcuts = KNOWNFOLDERID . FOLDERID_ApplicationShortcuts ,
2017-11-27 13:11:20 -05:00
/// <summary>Applications</summary>
2018-03-27 19:04:44 -04:00
AppsFolder = KNOWNFOLDERID . FOLDERID_AppsFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Installed Updates</summary>
2018-03-27 19:04:44 -04:00
AppUpdates = KNOWNFOLDERID . FOLDERID_AppUpdates ,
2017-11-27 13:11:20 -05:00
/// <summary>Camera Roll</summary>
2018-03-27 19:04:44 -04:00
CameraRoll = KNOWNFOLDERID . FOLDERID_CameraRoll ,
2017-11-27 13:11:20 -05:00
/// <summary>Temporary Burn Folder</summary>
2018-03-27 19:04:44 -04:00
CDBurning = KNOWNFOLDERID . FOLDERID_CDBurning ,
2017-11-27 13:11:20 -05:00
/// <summary>Programs and Features</summary>
2018-03-27 19:04:44 -04:00
ChangeRemovePrograms = KNOWNFOLDERID . FOLDERID_ChangeRemovePrograms ,
2017-11-27 13:11:20 -05:00
/// <summary>Administrative Tools</summary>
2018-03-27 19:04:44 -04:00
CommonAdminTools = KNOWNFOLDERID . FOLDERID_CommonAdminTools ,
2017-11-27 13:11:20 -05:00
/// <summary>OEM Links</summary>
2018-03-27 19:04:44 -04:00
CommonOEMLinks = KNOWNFOLDERID . FOLDERID_CommonOEMLinks ,
2017-11-27 13:11:20 -05:00
/// <summary>Programs</summary>
2018-03-27 19:04:44 -04:00
CommonPrograms = KNOWNFOLDERID . FOLDERID_CommonPrograms ,
2017-11-27 13:11:20 -05:00
/// <summary>Start Menu</summary>
2018-03-27 19:04:44 -04:00
CommonStartMenu = KNOWNFOLDERID . FOLDERID_CommonStartMenu ,
2017-11-27 13:11:20 -05:00
/// <summary>Startup</summary>
2018-03-27 19:04:44 -04:00
CommonStartup = KNOWNFOLDERID . FOLDERID_CommonStartup ,
2017-11-27 13:11:20 -05:00
/// <summary>Templates</summary>
2018-03-27 19:04:44 -04:00
CommonTemplates = KNOWNFOLDERID . FOLDERID_CommonTemplates ,
2017-11-27 13:11:20 -05:00
/// <summary>Computer</summary>
2018-03-27 19:04:44 -04:00
ComputerFolder = KNOWNFOLDERID . FOLDERID_ComputerFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Conflicts</summary>
2018-03-27 19:04:44 -04:00
ConflictFolder = KNOWNFOLDERID . FOLDERID_ConflictFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Network Connections</summary>
2018-03-27 19:04:44 -04:00
ConnectionsFolder = KNOWNFOLDERID . FOLDERID_ConnectionsFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Contacts</summary>
2018-03-27 19:04:44 -04:00
Contacts = KNOWNFOLDERID . FOLDERID_Contacts ,
2017-11-27 13:11:20 -05:00
/// <summary>Control Panel</summary>
2018-03-27 19:04:44 -04:00
ControlPanelFolder = KNOWNFOLDERID . FOLDERID_ControlPanelFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Cookies</summary>
2018-03-27 19:04:44 -04:00
Cookies = KNOWNFOLDERID . FOLDERID_Cookies ,
2017-11-27 13:11:20 -05:00
/// <summary>Desktop</summary>
2018-03-27 19:04:44 -04:00
Desktop = KNOWNFOLDERID . FOLDERID_Desktop ,
2017-11-27 13:11:20 -05:00
/// <summary>DeviceMetadataStore</summary>
2018-03-27 19:04:44 -04:00
DeviceMetadataStore = KNOWNFOLDERID . FOLDERID_DeviceMetadataStore ,
2017-11-27 13:11:20 -05:00
/// <summary>Documents</summary>
2018-03-27 19:04:44 -04:00
Documents = KNOWNFOLDERID . FOLDERID_Documents ,
2017-11-27 13:11:20 -05:00
/// <summary>Documents</summary>
2018-03-27 19:04:44 -04:00
DocumentsLibrary = KNOWNFOLDERID . FOLDERID_DocumentsLibrary ,
2017-11-27 13:11:20 -05:00
/// <summary>Downloads</summary>
2018-03-27 19:04:44 -04:00
Downloads = KNOWNFOLDERID . FOLDERID_Downloads ,
2017-11-27 13:11:20 -05:00
/// <summary>Favorites</summary>
2018-03-27 19:04:44 -04:00
Favorites = KNOWNFOLDERID . FOLDERID_Favorites ,
2017-11-27 13:11:20 -05:00
/// <summary>Fonts</summary>
2018-03-27 19:04:44 -04:00
Fonts = KNOWNFOLDERID . FOLDERID_Fonts ,
2017-11-27 13:11:20 -05:00
/// <summary>Games</summary>
2018-03-27 19:04:44 -04:00
Games = KNOWNFOLDERID . FOLDERID_Games ,
2017-11-27 13:11:20 -05:00
/// <summary>GameExplorer</summary>
2018-03-27 19:04:44 -04:00
GameTasks = KNOWNFOLDERID . FOLDERID_GameTasks ,
2017-11-27 13:11:20 -05:00
/// <summary>History</summary>
2018-03-27 19:04:44 -04:00
History = KNOWNFOLDERID . FOLDERID_History ,
2017-11-27 13:11:20 -05:00
/// <summary>Homegroup</summary>
2018-03-27 19:04:44 -04:00
HomeGroup = KNOWNFOLDERID . FOLDERID_HomeGroup ,
2017-11-27 13:11:20 -05:00
/// <summary>The user's username (%USERNAME%)</summary>
2018-03-27 19:04:44 -04:00
HomeGroupCurrentUser = KNOWNFOLDERID . FOLDERID_HomeGroupCurrentUser ,
2017-11-27 13:11:20 -05:00
/// <summary>ImplicitAppShortcuts</summary>
2018-03-27 19:04:44 -04:00
ImplicitAppShortcuts = KNOWNFOLDERID . FOLDERID_ImplicitAppShortcuts ,
2017-11-27 13:11:20 -05:00
/// <summary>Temporary Internet Files</summary>
2018-03-27 19:04:44 -04:00
InternetCache = KNOWNFOLDERID . FOLDERID_InternetCache ,
2017-11-27 13:11:20 -05:00
/// <summary>The Internet</summary>
2018-03-27 19:04:44 -04:00
InternetFolder = KNOWNFOLDERID . FOLDERID_InternetFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Libraries</summary>
2018-03-27 19:04:44 -04:00
Libraries = KNOWNFOLDERID . FOLDERID_Libraries ,
2017-11-27 13:11:20 -05:00
/// <summary>Links</summary>
2018-03-27 19:04:44 -04:00
Links = KNOWNFOLDERID . FOLDERID_Links ,
2017-11-27 13:11:20 -05:00
/// <summary>Local</summary>
2018-03-27 19:04:44 -04:00
LocalAppData = KNOWNFOLDERID . FOLDERID_LocalAppData ,
2017-11-27 13:11:20 -05:00
/// <summary>LocalLow</summary>
2018-03-27 19:04:44 -04:00
LocalAppDataLow = KNOWNFOLDERID . FOLDERID_LocalAppDataLow ,
2017-11-27 13:11:20 -05:00
/// <summary>None</summary>
2018-03-27 19:04:44 -04:00
LocalizedResourcesDir = KNOWNFOLDERID . FOLDERID_LocalizedResourcesDir ,
2017-11-27 13:11:20 -05:00
/// <summary>Music</summary>
2018-03-27 19:04:44 -04:00
Music = KNOWNFOLDERID . FOLDERID_Music ,
2017-11-27 13:11:20 -05:00
/// <summary>Music</summary>
2018-03-27 19:04:44 -04:00
MusicLibrary = KNOWNFOLDERID . FOLDERID_MusicLibrary ,
2017-11-27 13:11:20 -05:00
/// <summary>Network Shortcuts</summary>
2018-03-27 19:04:44 -04:00
NetHood = KNOWNFOLDERID . FOLDERID_NetHood ,
2017-11-27 13:11:20 -05:00
/// <summary>Network</summary>
2018-03-27 19:04:44 -04:00
NetworkFolder = KNOWNFOLDERID . FOLDERID_NetworkFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Original Images</summary>
2018-03-27 19:04:44 -04:00
OriginalImages = KNOWNFOLDERID . FOLDERID_OriginalImages ,
2017-11-27 13:11:20 -05:00
/// <summary>Slide Shows</summary>
2018-03-27 19:04:44 -04:00
PhotoAlbums = KNOWNFOLDERID . FOLDERID_PhotoAlbums ,
2017-11-27 13:11:20 -05:00
/// <summary>Pictures</summary>
2018-03-27 19:04:44 -04:00
PicturesLibrary = KNOWNFOLDERID . FOLDERID_PicturesLibrary ,
2017-11-27 13:11:20 -05:00
/// <summary>Pictures</summary>
2018-03-27 19:04:44 -04:00
Pictures = KNOWNFOLDERID . FOLDERID_Pictures ,
2017-11-27 13:11:20 -05:00
/// <summary>Playlists</summary>
2018-03-27 19:04:44 -04:00
Playlists = KNOWNFOLDERID . FOLDERID_Playlists ,
2017-11-27 13:11:20 -05:00
/// <summary>Printers</summary>
2018-03-27 19:04:44 -04:00
PrintersFolder = KNOWNFOLDERID . FOLDERID_PrintersFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Printer Shortcuts</summary>
2018-03-27 19:04:44 -04:00
PrintHood = KNOWNFOLDERID . FOLDERID_PrintHood ,
2017-11-27 13:11:20 -05:00
/// <summary>The user's username (%USERNAME%)</summary>
2018-03-27 19:04:44 -04:00
Profile = KNOWNFOLDERID . FOLDERID_Profile ,
2017-11-27 13:11:20 -05:00
/// <summary>ProgramData</summary>
2018-03-27 19:04:44 -04:00
ProgramData = KNOWNFOLDERID . FOLDERID_ProgramData ,
2017-11-27 13:11:20 -05:00
/// <summary>Program Files</summary>
2018-03-27 19:04:44 -04:00
ProgramFiles = KNOWNFOLDERID . FOLDERID_ProgramFiles ,
2017-11-27 13:11:20 -05:00
/// <summary>Program Files</summary>
2018-03-27 19:04:44 -04:00
ProgramFilesX64 = KNOWNFOLDERID . FOLDERID_ProgramFilesX64 ,
2017-11-27 13:11:20 -05:00
/// <summary>Program Files</summary>
2018-03-27 19:04:44 -04:00
ProgramFilesX86 = KNOWNFOLDERID . FOLDERID_ProgramFilesX86 ,
2017-11-27 13:11:20 -05:00
/// <summary>Common Files</summary>
2018-03-27 19:04:44 -04:00
ProgramFilesCommon = KNOWNFOLDERID . FOLDERID_ProgramFilesCommon ,
2017-11-27 13:11:20 -05:00
/// <summary>Common Files</summary>
2018-03-27 19:04:44 -04:00
ProgramFilesCommonX64 = KNOWNFOLDERID . FOLDERID_ProgramFilesCommonX64 ,
2017-11-27 13:11:20 -05:00
/// <summary>Common Files</summary>
2018-03-27 19:04:44 -04:00
ProgramFilesCommonX86 = KNOWNFOLDERID . FOLDERID_ProgramFilesCommonX86 ,
2017-11-27 13:11:20 -05:00
/// <summary>Programs</summary>
2018-03-27 19:04:44 -04:00
Programs = KNOWNFOLDERID . FOLDERID_Programs ,
2017-11-27 13:11:20 -05:00
/// <summary>Public</summary>
2018-03-27 19:04:44 -04:00
Public = KNOWNFOLDERID . FOLDERID_Public ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Desktop</summary>
2018-03-27 19:04:44 -04:00
PublicDesktop = KNOWNFOLDERID . FOLDERID_PublicDesktop ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Documents</summary>
2018-03-27 19:04:44 -04:00
PublicDocuments = KNOWNFOLDERID . FOLDERID_PublicDocuments ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Downloads</summary>
2018-03-27 19:04:44 -04:00
PublicDownloads = KNOWNFOLDERID . FOLDERID_PublicDownloads ,
2017-11-27 13:11:20 -05:00
/// <summary>GameExplorer</summary>
2018-03-27 19:04:44 -04:00
PublicGameTasks = KNOWNFOLDERID . FOLDERID_PublicGameTasks ,
2017-11-27 13:11:20 -05:00
/// <summary>Libraries</summary>
2018-03-27 19:04:44 -04:00
PublicLibraries = KNOWNFOLDERID . FOLDERID_PublicLibraries ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Music</summary>
2018-03-27 19:04:44 -04:00
PublicMusic = KNOWNFOLDERID . FOLDERID_PublicMusic ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Pictures</summary>
2018-03-27 19:04:44 -04:00
PublicPictures = KNOWNFOLDERID . FOLDERID_PublicPictures ,
2017-11-27 13:11:20 -05:00
/// <summary>Ringtones</summary>
2018-03-27 19:04:44 -04:00
PublicRingtones = KNOWNFOLDERID . FOLDERID_PublicRingtones ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Account Pictures</summary>
2018-03-27 19:04:44 -04:00
PublicUserTiles = KNOWNFOLDERID . FOLDERID_PublicUserTiles ,
2017-11-27 13:11:20 -05:00
/// <summary>Public Videos</summary>
2018-03-27 19:04:44 -04:00
PublicVideos = KNOWNFOLDERID . FOLDERID_PublicVideos ,
2017-11-27 13:11:20 -05:00
/// <summary>Quick Launch</summary>
2018-03-27 19:04:44 -04:00
QuickLaunch = KNOWNFOLDERID . FOLDERID_QuickLaunch ,
2017-11-27 13:11:20 -05:00
/// <summary>Recent Items</summary>
2018-03-27 19:04:44 -04:00
Recent = KNOWNFOLDERID . FOLDERID_Recent ,
2017-11-27 13:11:20 -05:00
/// <summary>Recorded TV</summary>
2018-03-27 19:04:44 -04:00
RecordedTVLibrary = KNOWNFOLDERID . FOLDERID_RecordedTVLibrary ,
2017-11-27 13:11:20 -05:00
/// <summary>Recycle Bin</summary>
2018-03-27 19:04:44 -04:00
RecycleBinFolder = KNOWNFOLDERID . FOLDERID_RecycleBinFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Resources</summary>
2018-03-27 19:04:44 -04:00
ResourceDir = KNOWNFOLDERID . FOLDERID_ResourceDir ,
2017-11-27 13:11:20 -05:00
/// <summary>Ringtones</summary>
2018-03-27 19:04:44 -04:00
Ringtones = KNOWNFOLDERID . FOLDERID_Ringtones ,
2017-11-27 13:11:20 -05:00
/// <summary>Roaming</summary>
2018-03-27 19:04:44 -04:00
RoamingAppData = KNOWNFOLDERID . FOLDERID_RoamingAppData ,
2017-11-27 13:11:20 -05:00
/// <summary>RoamedTileImages</summary>
2018-03-27 19:04:44 -04:00
RoamedTileImages = KNOWNFOLDERID . FOLDERID_RoamedTileImages ,
2017-11-27 13:11:20 -05:00
/// <summary>RoamingTiles</summary>
2018-03-27 19:04:44 -04:00
RoamingTiles = KNOWNFOLDERID . FOLDERID_RoamingTiles ,
2017-11-27 13:11:20 -05:00
/// <summary>Sample Music</summary>
2018-03-27 19:04:44 -04:00
SampleMusic = KNOWNFOLDERID . FOLDERID_SampleMusic ,
2017-11-27 13:11:20 -05:00
/// <summary>Sample Pictures</summary>
2018-03-27 19:04:44 -04:00
SamplePictures = KNOWNFOLDERID . FOLDERID_SamplePictures ,
2017-11-27 13:11:20 -05:00
/// <summary>Sample Playlists</summary>
2018-03-27 19:04:44 -04:00
SamplePlaylists = KNOWNFOLDERID . FOLDERID_SamplePlaylists ,
2017-11-27 13:11:20 -05:00
/// <summary>Sample Videos</summary>
2018-03-27 19:04:44 -04:00
SampleVideos = KNOWNFOLDERID . FOLDERID_SampleVideos ,
2017-11-27 13:11:20 -05:00
/// <summary>Saved Games</summary>
2018-03-27 19:04:44 -04:00
SavedGames = KNOWNFOLDERID . FOLDERID_SavedGames ,
2017-11-27 13:11:20 -05:00
/// <summary>Saved Pictures</summary>
2018-03-27 19:04:44 -04:00
SavedPictures = KNOWNFOLDERID . FOLDERID_SavedPictures ,
2017-11-27 13:11:20 -05:00
/// <summary>Saved Pictures Library</summary>
2018-03-27 19:04:44 -04:00
SavedPicturesLibrary = KNOWNFOLDERID . FOLDERID_SavedPicturesLibrary ,
2017-11-27 13:11:20 -05:00
/// <summary>Searches</summary>
2018-03-27 19:04:44 -04:00
SavedSearches = KNOWNFOLDERID . FOLDERID_SavedSearches ,
2017-11-27 13:11:20 -05:00
/// <summary>Screenshots</summary>
2018-03-27 19:04:44 -04:00
Screenshots = KNOWNFOLDERID . FOLDERID_Screenshots ,
2017-11-27 13:11:20 -05:00
/// <summary>Offline Files</summary>
2018-03-27 19:04:44 -04:00
SEARCH_CSC = KNOWNFOLDERID . FOLDERID_SEARCH_CSC ,
2017-11-27 13:11:20 -05:00
/// <summary>History</summary>
2018-03-27 19:04:44 -04:00
SearchHistory = KNOWNFOLDERID . FOLDERID_SearchHistory ,
2017-11-27 13:11:20 -05:00
/// <summary>Search Results</summary>
2018-03-27 19:04:44 -04:00
SearchHome = KNOWNFOLDERID . FOLDERID_SearchHome ,
2017-11-27 13:11:20 -05:00
/// <summary>Microsoft Office Outlook</summary>
2018-03-27 19:04:44 -04:00
SEARCH_MAPI = KNOWNFOLDERID . FOLDERID_SEARCH_MAPI ,
2017-11-27 13:11:20 -05:00
/// <summary>Templates</summary>
2018-03-27 19:04:44 -04:00
SearchTemplates = KNOWNFOLDERID . FOLDERID_SearchTemplates ,
2017-11-27 13:11:20 -05:00
/// <summary>SendTo</summary>
2018-03-27 19:04:44 -04:00
SendTo = KNOWNFOLDERID . FOLDERID_SendTo ,
2017-11-27 13:11:20 -05:00
/// <summary>Gadgets</summary>
2018-03-27 19:04:44 -04:00
SidebarDefaultParts = KNOWNFOLDERID . FOLDERID_SidebarDefaultParts ,
2017-11-27 13:11:20 -05:00
/// <summary>Gadgets</summary>
2018-03-27 19:04:44 -04:00
SidebarParts = KNOWNFOLDERID . FOLDERID_SidebarParts ,
2017-11-27 13:11:20 -05:00
/// <summary>OneDrive</summary>
2018-03-27 19:04:44 -04:00
SkyDrive = KNOWNFOLDERID . FOLDERID_SkyDrive ,
2017-11-27 13:11:20 -05:00
/// <summary>Camera Roll</summary>
2018-03-27 19:04:44 -04:00
SkyDriveCameraRoll = KNOWNFOLDERID . FOLDERID_SkyDriveCameraRoll ,
2017-11-27 13:11:20 -05:00
/// <summary>Documents</summary>
2018-03-27 19:04:44 -04:00
SkyDriveDocuments = KNOWNFOLDERID . FOLDERID_SkyDriveDocuments ,
2017-11-27 13:11:20 -05:00
/// <summary>Pictures</summary>
2018-03-27 19:04:44 -04:00
SkyDrivePictures = KNOWNFOLDERID . FOLDERID_SkyDrivePictures ,
2017-11-27 13:11:20 -05:00
/// <summary>Start Menu</summary>
2018-03-27 19:04:44 -04:00
StartMenu = KNOWNFOLDERID . FOLDERID_StartMenu ,
2017-11-27 13:11:20 -05:00
/// <summary>Startup</summary>
2018-03-27 19:04:44 -04:00
Startup = KNOWNFOLDERID . FOLDERID_Startup ,
2017-11-27 13:11:20 -05:00
/// <summary>Sync Center</summary>
2018-03-27 19:04:44 -04:00
SyncManagerFolder = KNOWNFOLDERID . FOLDERID_SyncManagerFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Sync Results</summary>
2018-03-27 19:04:44 -04:00
SyncResultsFolder = KNOWNFOLDERID . FOLDERID_SyncResultsFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>Sync Setup</summary>
2018-03-27 19:04:44 -04:00
SyncSetupFolder = KNOWNFOLDERID . FOLDERID_SyncSetupFolder ,
2017-11-27 13:11:20 -05:00
/// <summary>System32</summary>
2018-03-27 19:04:44 -04:00
System = KNOWNFOLDERID . FOLDERID_System ,
2017-11-27 13:11:20 -05:00
/// <summary>System32</summary>
2018-03-27 19:04:44 -04:00
SystemX86 = KNOWNFOLDERID . FOLDERID_SystemX86 ,
2017-11-27 13:11:20 -05:00
/// <summary>Templates</summary>
2018-03-27 19:04:44 -04:00
Templates = KNOWNFOLDERID . FOLDERID_Templates ,
2017-11-27 13:11:20 -05:00
/// <summary>User Pinned</summary>
2018-03-27 19:04:44 -04:00
UserPinned = KNOWNFOLDERID . FOLDERID_UserPinned ,
2017-11-27 13:11:20 -05:00
/// <summary>Users</summary>
2018-03-27 19:04:44 -04:00
UserProfiles = KNOWNFOLDERID . FOLDERID_UserProfiles ,
2017-11-27 13:11:20 -05:00
/// <summary>Programs</summary>
2018-03-27 19:04:44 -04:00
UserProgramFiles = KNOWNFOLDERID . FOLDERID_UserProgramFiles ,
2017-11-27 13:11:20 -05:00
/// <summary>Programs</summary>
2018-03-27 19:04:44 -04:00
UserProgramFilesCommon = KNOWNFOLDERID . FOLDERID_UserProgramFilesCommon ,
2017-11-27 13:11:20 -05:00
/// <summary>The user's full name (for instance, Jean Philippe Bagel) entered when the user account was created.</summary>
2018-03-27 19:04:44 -04:00
UsersFiles = KNOWNFOLDERID . FOLDERID_UsersFiles ,
2017-11-27 13:11:20 -05:00
/// <summary>Libraries</summary>
2018-03-27 19:04:44 -04:00
UsersLibraries = KNOWNFOLDERID . FOLDERID_UsersLibraries ,
2017-11-27 13:11:20 -05:00
/// <summary>Videos</summary>
2018-03-27 19:04:44 -04:00
Videos = KNOWNFOLDERID . FOLDERID_Videos ,
2017-11-27 13:11:20 -05:00
/// <summary>Videos</summary>
2018-03-27 19:04:44 -04:00
VideosLibrary = KNOWNFOLDERID . FOLDERID_VideosLibrary ,
2017-11-27 13:11:20 -05:00
/// <summary>Windows</summary>
2018-03-27 19:04:44 -04:00
Windows = KNOWNFOLDERID . FOLDERID_Windows ,
2017-11-27 13:11:20 -05:00
/// <summary>Undefined</summary>
Undefined = 0xFFFF ,
}
/// <summary>Class to let the user browse for a folder.</summary>
[ToolboxBitmap(typeof(FolderBrowserDialog), "Dialog"), Description("Dialog that browses network computers.")]
public class FolderBrowserDialog : CommonDialog
{
2018-03-27 19:04:44 -04:00
private const KnownFolder defaultComputersFolder = KnownFolder . NetworkFolder ;
private const KnownFolder defaultFolderFolder = KnownFolder . ComputerFolder ;
private const KnownFolder defaultPrintersFolder = KnownFolder . PrintersFolder ;
2017-11-27 13:11:20 -05:00
private FolderBrowserDialogOptions browseOption ;
private bool initialized ;
2018-03-27 19:04:44 -04:00
private KnownFolder rootFolder ;
2017-11-27 13:11:20 -05:00
private PIDL rootPidl ;
/// <summary>Initializes a new instance of the <see cref="FolderBrowserDialog"/> class.</summary>
public FolderBrowserDialog ( )
{
Reset ( ) ;
RootFolder = defaultFolderFolder ;
}
/// <summary>Occurs when dialog box has been initialized and primary values have been set.</summary>
public event EventHandler < FolderBrowserDialogInitializedEventArgs > Initialized ;
/// <summary>Event that is raised when the user selects an invalid folder.</summary>
public event EventHandler < InvalidFolderEventArgs > InvalidFolderSelected ;
/// <summary>Occurs when <see cref="SelectedItem"/> property has changed.</summary>
public event PropertyChangedEventHandler SelectedItemChanged ;
/// <summary>Gets or sets the types of items to browse.</summary>
[ DefaultValue ( typeof ( FolderBrowserDialogOptions ) , "Folders" ) , Localizable ( false ) , Category ( "Behavior" ) ,
Description ( "The types of items to browse" ) ]
public FolderBrowserDialogOptions BrowseOption
{
get = > browseOption ; set
{
if ( browseOption ! = value )
{
browseOption = value ;
switch ( browseOption )
{
case FolderBrowserDialogOptions . Folders :
case FolderBrowserDialogOptions . FoldersAndFiles :
if ( RootFolder = = defaultComputersFolder | | RootFolder = = defaultPrintersFolder )
RootFolder = defaultFolderFolder ;
break ;
case FolderBrowserDialogOptions . Computers :
RootFolder = defaultComputersFolder ;
break ;
case FolderBrowserDialogOptions . Printers :
RootFolder = defaultPrintersFolder ;
break ;
default :
throw new ArgumentOutOfRangeException ( ) ;
}
}
}
}
/// <summary>Gets or sets the caption of the dialog box.</summary>
[DefaultValue(""), Category("Appearance"), Localizable(true), Description("Caption of the dialog box.")]
public string Caption { get ; set ; } = "" ;
/// <summary>Gets or sets the description shown to the user.</summary>
[DefaultValue(""), Category("Appearance"), Localizable(true), Description("Description shown to the user.")]
public string Description { get ; set ; } = "" ;
/// <summary>Gets or sets whether to automatically expand the tree when shown.</summary>
[DefaultValue(true), Localizable(false), Category("Behavior"), Description("Whether to automatically expand the tree when shown.")]
public bool Expanded { get ; set ; } = true ;
/// <summary>Gets or sets whether to hide network folders below the domain level in the tree.</summary>
[DefaultValue(false), Localizable(false), Category("Behavior"), Description("Whether to hide network folders below the domain.")]
public bool HideDomainFolders { get ; set ; }
/// <summary>Gets or sets whether to return only file system folders.</summary>
[DefaultValue(false), Localizable(false), Category("Behavior"), Description("Whether to return only file system folders.")]
public bool LocalFileSystemOnly { get ; set ; }
/// <summary>Gets or sets the text on the OK button.</summary>
[DefaultValue(""), Category("Appearance"), Localizable(true), Description("Text on the OK button.")]
public string OkText { get ; set ; } = "" ;
/// <summary>Gets or sets the root folder.</summary>
[Localizable(false), Category("Data"), Description("Root folder of tree."), DefaultValue(defaultFolderFolder)]
2018-03-27 19:04:44 -04:00
public KnownFolder RootFolder
2017-11-27 13:11:20 -05:00
{
get = > rootFolder ; set
{
rootFolder = value ;
try { rootPidl = ( ( KNOWNFOLDERID ) RootFolder ) . PIDL ( ) ; }
catch
{
System . Diagnostics . Debug . WriteLine ( $"The known folder '{RootFolder}' is not supported for this OS or application configuration." ) ;
ResetRootFolder ( ) ;
}
}
}
/// <summary>Gets or sets the PIDL associated with the root folder. This can be used to specify a non-known folder as the root.</summary>
/// <value>The root folder's PIDL.</value>
[DefaultValue(0), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
// ReSharper disable once InconsistentNaming
public PIDL RootFolderPIDL
{
get = > rootPidl ; set
{
rootPidl = value ;
2018-03-27 19:04:44 -04:00
rootFolder = KnownFolder . Undefined ;
2017-11-27 13:11:20 -05:00
}
}
2018-03-27 19:04:44 -04:00
/// <summary>Gets the path or name of the folder selected by the user.</summary>
[DefaultValue(""), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string SelectedItem { get ; private set ; } = "" ;
2017-11-27 13:11:20 -05:00
/// <summary>Gets the image from the system image list associated with the selected item.</summary>
/// <value>The selected item's image.</value>
[DefaultValue(null), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2018-09-22 00:35:34 -04:00
public Icon SelectedItemImage = > SelectedItemPIDL . GetIcon ( IconSize . Small ) ;
2017-11-27 13:11:20 -05:00
/// <summary>Gets the PIDL associated with the selected item.</summary>
/// <value>The selected item's PIDL.</value>
[DefaultValue(0), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
// ReSharper disable once InconsistentNaming
2018-09-22 00:35:34 -04:00
public PIDL SelectedItemPIDL { get ; private set ; }
2017-11-27 13:11:20 -05:00
/// <summary>Gets or sets whether or not to show file junctions, such as a library or compressed file. This only is available in Windows 7 and later.</summary>
[DefaultValue(false), Localizable(false), Category("Behavior"), Description("Whether or not to show file junctions.")]
public bool ShowFileJunctions { get ; set ; }
/// <summary>Gets or sets whether or not to show an edit box for the folder path.</summary>
[DefaultValue(false), Localizable(false), Category("Behavior"), Description("Whether or not to show an edit box for the folder.")]
public bool ShowFolderPathEditBox { get ; set ; }
/// <summary>Gets or sets whether or not to show the new folder button.</summary>
[DefaultValue(false), Localizable(false), Category("Behavior"), Description("Whether or not to show the new folder button.")]
public bool ShowNewFolderButton { get ; set ; }
/// <summary>When overridden in a derived class, resets the properties of a common dialog box to their default values.</summary>
public override void Reset ( )
{
BrowseOption = FolderBrowserDialogOptions . Folders ;
Caption = Description = OkText = SelectedItem = string . Empty ;
Expanded = true ;
RootFolder = defaultFolderFolder ;
HideDomainFolders = ShowFileJunctions = LocalFileSystemOnly = ShowFolderPathEditBox = ShowNewFolderButton = initialized = false ;
2018-09-22 00:35:34 -04:00
SelectedItemPIDL = null ;
2017-11-27 13:11:20 -05:00
}
/// <summary>Shows the dialog box to let the user browse for and select a folder.</summary>
/// <param name="parentWindowHandle">The HWND of the parent window.</param>
/// <returns>The selected folder or <c>null</c> if no folder was selected by the user.</returns>
protected override bool RunDialog ( IntPtr parentWindowHandle )
{
// Setup BROWSEINFO.dwFlag value
EnumFlagIndexer < BrowseInfoFlag > browseInfoFlag = BrowseInfoFlag . BIF_SHAREABLE ;
browseInfoFlag [ BrowseInfoFlag . BIF_NEWDIALOGSTYLE ] = Application . OleRequired ( ) = = ApartmentState . STA ;
browseInfoFlag [ BrowseInfoFlag . BIF_DONTGOBELOWDOMAIN ] = HideDomainFolders ;
browseInfoFlag [ BrowseInfoFlag . BIF_BROWSEFILEJUNCTIONS ] = ShowFileJunctions ;
browseInfoFlag [ BrowseInfoFlag . BIF_RETURNONLYFSDIRS ] = LocalFileSystemOnly ;
browseInfoFlag [ BrowseInfoFlag . BIF_NONEWFOLDERBUTTON ] = ! ShowNewFolderButton ;
browseInfoFlag [ BrowseInfoFlag . BIF_EDITBOX | BrowseInfoFlag . BIF_VALIDATE ] = ShowFolderPathEditBox ;
switch ( BrowseOption )
{
case FolderBrowserDialogOptions . Folders :
break ;
case FolderBrowserDialogOptions . FoldersAndFiles :
browseInfoFlag | = BrowseInfoFlag . BIF_BROWSEINCLUDEFILES ;
break ;
case FolderBrowserDialogOptions . Computers :
browseInfoFlag | = BrowseInfoFlag . BIF_BROWSEFORCOMPUTER ;
RootFolder = defaultComputersFolder ;
break ;
case FolderBrowserDialogOptions . Printers :
browseInfoFlag | = BrowseInfoFlag . BIF_BROWSEFORPRINTER ;
RootFolder = defaultPrintersFolder ;
break ;
default :
throw new ArgumentOutOfRangeException ( ) ;
}
// Setup the BROWSEINFO structure
var dn = new SafeCoTaskMemString ( MAX_PATH ) ;
var bi = new BROWSEINFO ( parentWindowHandle , rootPidl . DangerousGetHandle ( ) , Description , browseInfoFlag , OnBrowseEvent , dn ) ;
// Show the dialog
2018-11-19 23:18:50 -05:00
SelectedItemPIDL = SHBrowseForFolder ( bi ) ;
2018-09-22 00:35:34 -04:00
if ( SelectedItemPIDL . IsInvalid ) return false ;
2017-11-27 13:11:20 -05:00
if ( browseInfoFlag [ BrowseInfoFlag . BIF_BROWSEFORPRINTER ] | | browseInfoFlag [ BrowseInfoFlag . BIF_BROWSEFORCOMPUTER ] )
SelectedItem = bi . DisplayName ;
else
2018-09-22 00:35:34 -04:00
SelectedItem = GetNameForPidl ( SelectedItemPIDL ) ;
2017-11-27 13:11:20 -05:00
return true ;
}
/// <summary>Enables or disables the OK button in the dialog box.</summary>
/// <param name="hwnd">The hwnd of the dialog box.</param>
/// <param name="isEnabled">Whether or not the OK button should be enabled.</param>
2018-09-22 00:35:34 -04:00
private static void EnableOk ( HWND hwnd , bool isEnabled ) = > SendMessage ( hwnd , ( uint ) BrowseForFolderMessages . BFFM_ENABLEOK , ( IntPtr ) 0 , ( IntPtr ) ( isEnabled ? 1 : 0 ) ) ;
2017-11-27 13:11:20 -05:00
private static string GetNameForPidl ( PIDL pidl )
{
2018-11-19 23:18:50 -05:00
try { SHGetNameFromIDList ( pidl , SIGDN . SIGDN_FILESYSPATH , out var mStr ) ; return mStr ; } catch { }
try { SHGetNameFromIDList ( pidl , SIGDN . SIGDN_DESKTOPABSOLUTEEDITING , out var mStr ) ; return mStr ; } catch { }
try { SHGetNameFromIDList ( pidl , SIGDN . SIGDN_NORMALDISPLAY , out var mStr ) ; return mStr ; } catch { }
2017-11-27 13:11:20 -05:00
return string . Empty ;
}
private static Icon GetSystemImageListIcon ( int idx )
{
// Test first for overridden icon images
var regVal = Registry . GetValue ( @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" , idx . ToString ( ) , null ) ;
if ( regVal ! = null )
return ResourceFile . GetResourceIcon ( regVal . ToString ( ) ) ;
// Get the image from the system image list
try
{
2018-09-22 00:35:34 -04:00
return IconExtension . GetSystemIcon ( System . IO . Path . GetPathRoot ( Environment . SystemDirectory ) , IconSize . Small ) ;
}
catch
{
return null ;
2017-11-27 13:11:20 -05:00
}
}
/// <summary>Callback for Windows.</summary>
/// <param name="hwnd">Window handle of the browse dialog box.</param>
/// <param name="uMsg">Dialog box event that generated the statusMessage.</param>
/// <param name="lParam">Value whose meaning depends on the event specified in uMsg.</param>
/// <param name="lpData">Application-defined value that was specified in the lParam member of the BROWSEINFO structure used in the call to SHBrowseForFolder.</param>
/// <returns>
/// Returns 0 except in the case of BFFM_VALIDATEFAILED. For that flag, returns 0 to dismiss the dialog or nonzero to keep the dialog displayed.
/// </returns>
//[CLSCompliant(false)]
2018-10-26 14:24:07 -04:00
private int OnBrowseEvent ( HWND hwnd , BrowseForFolderMessages uMsg , IntPtr lParam , IntPtr lpData )
2017-11-27 13:11:20 -05:00
{
var messsage = uMsg ;
switch ( messsage )
{
case BrowseForFolderMessages . BFFM_INITIALIZED :
// Dialog is being initialized, so set the initial parameters
if ( ! string . IsNullOrEmpty ( Caption ) )
2018-09-22 00:35:34 -04:00
SetWindowText ( hwnd , Caption ) ;
2017-11-27 13:11:20 -05:00
if ( ! string . IsNullOrEmpty ( SelectedItem ) )
2018-09-22 00:35:34 -04:00
SendMessage ( hwnd , ( uint ) BrowseForFolderMessages . BFFM_SETSELECTIONW , ( IntPtr ) 1 , SelectedItem ) ;
2017-11-27 13:11:20 -05:00
if ( Expanded )
2018-09-22 00:35:34 -04:00
SendMessage ( hwnd , ( uint ) BrowseForFolderMessages . BFFM_SETEXPANDED , ( IntPtr ) 1 , rootPidl . DangerousGetHandle ( ) ) ;
2017-11-27 13:11:20 -05:00
if ( ! string . IsNullOrEmpty ( OkText ) )
2018-09-22 00:35:34 -04:00
SendMessage ( hwnd , ( uint ) BrowseForFolderMessages . BFFM_SETOKTEXT , ( IntPtr ) 0 , OkText ) ;
2017-11-27 13:11:20 -05:00
Initialized ? . Invoke ( this , new FolderBrowserDialogInitializedEventArgs ( hwnd ) ) ;
initialized = true ;
return 0 ;
case BrowseForFolderMessages . BFFM_SELCHANGED :
try
{
2018-09-22 00:35:34 -04:00
if ( ! initialized | | SelectedItemPIDL ? . DangerousGetHandle ( ) = = lParam ) return 0 ;
2017-11-27 13:11:20 -05:00
var tmpPidl = new PIDL ( lParam , false , false ) ;
var str = GetNameForPidl ( tmpPidl ) ;
if ( string . IsNullOrEmpty ( str ) )
return 0 ;
SelectedItem = str ;
2018-09-22 00:35:34 -04:00
SelectedItemPIDL = tmpPidl ;
2017-11-27 13:11:20 -05:00
}
catch
{
return 0 ;
}
SelectedItemChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( SelectedItem ) ) ) ;
return 0 ;
case BrowseForFolderMessages . BFFM_VALIDATEFAILEDA :
case BrowseForFolderMessages . BFFM_VALIDATEFAILEDW :
if ( InvalidFolderSelected ! = null )
{
var folderName = messsage = = BrowseForFolderMessages . BFFM_VALIDATEFAILEDA ? Marshal . PtrToStringAnsi ( lParam ) : Marshal . PtrToStringUni ( lParam ) ;
var e = new InvalidFolderEventArgs ( folderName , true ) ;
InvalidFolderSelected ? . Invoke ( this , e ) ;
return e . DismissDialog ? 0 : 1 ;
}
return 0 ;
default :
return 0 ;
}
}
private void ResetRootFolder ( )
{
switch ( BrowseOption )
{
case FolderBrowserDialogOptions . Folders :
case FolderBrowserDialogOptions . FoldersAndFiles :
RootFolder = defaultFolderFolder ;
break ;
case FolderBrowserDialogOptions . Computers :
RootFolder = defaultComputersFolder ;
break ;
case FolderBrowserDialogOptions . Printers :
RootFolder = defaultPrintersFolder ;
break ;
default :
throw new ArgumentOutOfRangeException ( ) ;
}
}
private bool ShouldSerializeRootFolder ( )
{
switch ( BrowseOption )
{
case FolderBrowserDialogOptions . Folders :
case FolderBrowserDialogOptions . FoldersAndFiles :
2018-03-27 19:04:44 -04:00
return RootFolder ! = KnownFolder . ComputerFolder ;
2017-11-27 13:11:20 -05:00
case FolderBrowserDialogOptions . Computers :
2018-03-27 19:04:44 -04:00
return RootFolder ! = KnownFolder . NetworkFolder ;
2017-11-27 13:11:20 -05:00
case FolderBrowserDialogOptions . Printers :
2018-03-27 19:04:44 -04:00
return RootFolder ! = KnownFolder . PrintersFolder ;
2017-11-27 13:11:20 -05:00
default :
throw new ArgumentOutOfRangeException ( ) ;
}
}
}
/// <summary>Event arguments for when the <see cref="FolderBrowserDialog"/> has been initialized.</summary>
public class FolderBrowserDialogInitializedEventArgs : EventArgs
{
/// <summary>The HWND of the dialog box.</summary>
2018-10-26 14:24:07 -04:00
public readonly HWND hwnd ;
2017-11-27 13:11:20 -05:00
/// <summary>Initializes a new instance of the <see cref="FolderBrowserDialogInitializedEventArgs"/> class.</summary>
/// <param name="hwnd">The HWND of the dialog box.</param>
2018-10-26 14:24:07 -04:00
public FolderBrowserDialogInitializedEventArgs ( HWND hwnd ) { this . hwnd = hwnd ; }
2017-11-27 13:11:20 -05:00
}
/// <summary>Event arguments for when an invalid folder is selected.</summary>
public class InvalidFolderEventArgs : EventArgs
{
/// <summary>Constructs an instance.</summary>
/// <param name="folderName">The name of the invalid folder.</param>
/// <param name="dismissDialog">Whether or not to dismiss the dialog.</param>
public InvalidFolderEventArgs ( string folderName , bool dismissDialog )
{
FolderName = folderName ;
DismissDialog = dismissDialog ;
}
/// <summary>Gets or sets whether or not to dismiss the dialog box.</summary>
public bool DismissDialog { get ; set ; }
/// <summary>Gets the name of the invalid folder.</summary>
public string FolderName { get ; }
}
}