visual studio 2008 - C# How to allow selection of My Computer with folderBrowserDialog -


in application, want folderbrowserdialog allow selection of my computer. ok button gets disabled after selecting my computer dialog box.

is there way allow selection of my computer in browse dialog?

my computer (or pc in more recent windows versions) special folder (not file system folder), , standard folderbrowserdialog class not support it.

here replacement sample folderbrowser class allows user select folder. example uses in windows form app:

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void button1_click(object sender, eventargs e)     {         // other special guids defined in windows sdk's shlguid.h         guid clsid_mycomputer = new guid("20d04fe0-3aea-1069-a2d8-08002b30309d");          folderbrowser dlg = new folderbrowser();         dlg.title = "choose folder want";          // optionally uncomment following line start folder         //dlg.selectedpath = @"c:\temp";          // optionally uncomment following line start computer/this pc         //dlg.selecteddesktopabsoluteparsing = "::" + clsid_mycomputer.tostring("b");          if (dlg.showdialog(null) == dialogresult.ok)         {             messagebox.show(dlg.selecteddesktopabsoluteparsing + environment.newline +                 dlg.selectednormaldisplay + environment.newline +                 dlg.selectedpath + environment.newline +                 dlg.selectedurl);              if (dlg.selecteddesktopabsoluteparsing == "::" + clsid_mycomputer.tostring("b").toupperinvariant())             {                 messagebox.show("my computer selected!");             }         }     } } 

and @ bottom of answer you'll find folderbrowser replacement class. interesting part line:

dialog.setoptions(fos.fos_pickfolders | fos.fos_allnonstorageitems); 

which instruct code pick folders , allow non file system items.

for normal folders, returned selectedpath property contain folder path. special folders without storage, empty. shell provides canonical moniker contains folder path normal folders , special values other folders defined in selecteddesktopabsoluteparsing property. in case of computer, value "::{20d04fe0-3aea-1069-a2d8-08002b30309d}".

this special syntax defined officially here: specifying namespace extension's location.

public class folderbrowser {     public string selectedpath { get; set; }     public string selecteddesktopabsoluteparsing { get; set; }     public string title { get; set; }     public string selectednormaldisplay { get; private set; }     public string selectedurl { get; private set; }      public dialogresult showdialog(iwin32window owner)     {         ishellitem result = null;         ifileopendialog dialog = (ifileopendialog)new fileopendialog();          if (!string.isnullorempty(selectedpath))         {             selectinitialpath(dialog, selectedpath);         }         else if (!string.isnullorempty(selecteddesktopabsoluteparsing))         {             selectinitialpath(dialog, selecteddesktopabsoluteparsing);         }          if (!string.isnullorwhitespace(title))         {             dialog.settitle(title);         }          dialog.setoptions(fos.fos_pickfolders | fos.fos_allnonstorageitems);         uint hr = dialog.show(owner != null ? owner.handle : intptr.zero);         if (hr == error_cancelled)             return dialogresult.cancel;          if (hr != 0)             return dialogresult.abort;          dialog.getresult(out result);          string path;         result.getdisplayname(sigdn.sigdn_filesyspath, out path);         selectedpath = path;          result.getdisplayname(sigdn.sigdn_normaldisplay, out path);         selectednormaldisplay = path;          result.getdisplayname(sigdn.sigdn_desktopabsoluteparsing, out path);         selecteddesktopabsoluteparsing = path;          result.getdisplayname(sigdn.sigdn_url, out path);         selectedurl = path;          return dialogresult.ok;     }      private void selectinitialpath(ifileopendialog dialog, string path)     {         uint atts = 0;         intptr idl = intptr.zero;         if (shilcreatefrompath(path, out idl, ref atts) == 0)         {             ishellitem initial = null;             if (shcreateshellitem(intptr.zero, intptr.zero, idl, out initial) == 0)             {                 dialog.setfolder(initial);             }             marshal.freecotaskmem(idl);         }     }      [dllimport("shell32.dll", charset = charset.unicode)]     private static extern int shilcreatefrompath(string pszpath, out intptr ppidl, ref uint rgflnout);      [dllimport("shell32.dll")]     private static extern int shcreateshellitem(intptr pidlparent, intptr psfparent, intptr pidl, out ishellitem ppsi);      private const uint error_cancelled = 0x800704c7;      [comimport]     [guid("dc1c5a9c-e88a-4dde-a5a1-60f82a20aef7")]     private class fileopendialog     {     }      [guid("42f85136-db7e-439c-85f1-e4075d135fc8")]     [interfacetype(cominterfacetype.interfaceisiunknown)]     private interface ifileopendialog     {         [preservesig]         uint show(intptr parent); // imodalwindow         void setfiletypes();  // not defined         void setfiletypeindex(uint ifiletype);         void getfiletypeindex(out uint pifiletype);         void advise(); // not defined         void unadvise();         void setoptions(fos fos);         void getoptions(out fos pfos);         void setdefaultfolder(ishellitem psi);         void setfolder(ishellitem psi);         void getfolder(out ishellitem ppsi);         void getcurrentselection(out ishellitem ppsi);         void setfilename([marshalas(unmanagedtype.lpwstr)] string pszname);         void getfilename([marshalas(unmanagedtype.lpwstr)] out string pszname);         void settitle([marshalas(unmanagedtype.lpwstr)] string psztitle);         void setokbuttonlabel([marshalas(unmanagedtype.lpwstr)] string psztext);         void setfilenamelabel([marshalas(unmanagedtype.lpwstr)] string pszlabel);         void getresult(out ishellitem ppsi);         void addplace(ishellitem psi, int alignment);         void setdefaultextension([marshalas(unmanagedtype.lpwstr)] string pszdefaultextension);         void close(int hr);         void setclientguid();  // not defined         void clearclientdata();         void setfilter([marshalas(unmanagedtype.interface)] intptr pfilter);         void getresults([marshalas(unmanagedtype.interface)] out intptr ppenum); // not defined         void getselecteditems([marshalas(unmanagedtype.interface)] out intptr ppsai); // not defined     }      [guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]     [interfacetype(cominterfacetype.interfaceisiunknown)]     private interface ishellitem     {         void bindtohandler(); // not defined         void getparent(); // not defined         [preservesig]         int getdisplayname(sigdn sigdnname, [marshalas(unmanagedtype.lpwstr)] out string ppszname);         void getattributes();  // not defined         void compare();  // not defined     }      // https://msdn.microsoft.com/en-us/library/windows/desktop/bb762544.aspx     private enum sigdn : uint     {         sigdn_desktopabsoluteediting = 0x8004c000,         sigdn_desktopabsoluteparsing = 0x80028000,         sigdn_filesyspath = 0x80058000,         sigdn_normaldisplay = 0,         sigdn_parentrelative = 0x80080001,         sigdn_parentrelativeediting = 0x80031001,         sigdn_parentrelativeforaddressbar = 0x8007c001,         sigdn_parentrelativeparsing = 0x80018001,         sigdn_url = 0x80068000     }      // https://msdn.microsoft.com/en-us/library/windows/desktop/dn457282.aspx     [flags]     private enum fos     {         fos_allnonstorageitems = 0x80,         fos_allowmultiselect = 0x200,         fos_createprompt = 0x2000,         fos_defaultnominimode = 0x20000000,         fos_dontaddtorecent = 0x2000000,         fos_filemustexist = 0x1000,         fos_forcefilesystem = 0x40,         fos_forceshowhidden = 0x10000000,         fos_hidemruplaces = 0x20000,         fos_hidepinnedplaces = 0x40000,         fos_nochangedir = 8,         fos_nodereferencelinks = 0x100000,         fos_noreadonlyreturn = 0x8000,         fos_notestfilecreate = 0x10000,         fos_novalidate = 0x100,         fos_overwriteprompt = 2,         fos_pathmustexist = 0x800,         fos_pickfolders = 0x20,         fos_shareaware = 0x4000,         fos_strictfiletypes = 4     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -