Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using the following code to launch a folder dialog and user can select any folder.

C++
BROWSEINFO   bi;
   ZeroMemory(&bi,   sizeof(bi));
   TCHAR   szDisplayName[MAX_PATH];
   szDisplayName[0]    =   'a';

   bi.hwndOwner        =   NULL;
   bi.pidlRoot         =   NULL;
   bi.pszDisplayName   =   szDisplayName;
   bi.lpszTitle        =   _T("Please select a folder to locate your PGA :");
   bi.ulFlags          =   BIF_RETURNONLYFSDIRS;
   bi.lParam           =   NULL;
   bi.iImage           =   0;

   LPITEMIDLIST   pidl   =   SHBrowseForFolder(&bi);
   TCHAR   szPathName[MAX_PATH];
   if   (NULL   !=   pidl)
   {
        BOOL bRet = SHGetPathFromIDList(pidl,szPathName);
        if(FALSE == bRet)
        {
             return;
        }
        else
        {
          PGA_PATH.Clear();
          PGA_PATH.SetWindowTextA(szPathName); //PGA_PATH name of the text box
          SetValudata(szPathName); //SetValudata saves the path selected by the user in a file .

        }
   }

But now new requirement came into picture and which states that do not allow the user to select a specific folder.

for Example if User tries to select a folder named PGA then do not allow user to select PGA folder.
Path string---> C:\PGA or 
                C:\Users\mm232\Pictures\PGA 
                C:\Users\mm232\PGA\Pictures


do not let the user select such type path. or disable the selection.
Posted
Updated 19-Jul-15 21:17pm
v2
Comments
Mukesh Pr@sad 20-Jul-15 3:44am    
After selection of the folder..you can check for restricted folder, if found you can stop the further execution and can force the user to browse another folder.
Richard MacCutchan 20-Jul-15 4:18am    
Why are you using SHBrowseForFolder if the user is not allowed to choose? Just use the functions to navigate to the folder where the files will be accessed from.
Thakur JAI SINGH 20-Jul-15 4:26am    
@Mukesh Pr@sad I can do that but I want to do it programatically.
Afzaal Ahmad Zeeshan 20-Jul-15 5:36am    
Reply to their comment to notify them. @ doesn't work.
Thakur JAI SINGH 20-Jul-15 4:28am    
@Richard MacCutchan I want to restrict user for just one folder.... not all folder

You can do any validation you want. In most cases such requirement does not make much senses.

If your application uses some folders where user should not save its own file, it would probably be best to use a subfolder of application data. If a single folder should be used then why ask the user. If you want only a few specific locations, then it might be preferable to only show a list of predefined locations...

Alternatively, setting folder permission might be usefull if you want to protect a folder from accidental user changes but I won't go that far.

Be aware that the user can always move file manually so it might not worth to invest to much time to make such validations as it can be easily bypassed by copying files once they are saved.

Also restricting to a folder that don't contains PGA could be too restrictive. Maybe one user would have those initials and uses that for its user name... Or there might be a folder using that name for something not at all related to your application.

If you still want to do restrictions, I would suggest that you only prevent specific full path names and not arbitrary path containing some text.
 
Share this answer
 
Check out this article here on CodeProject. I think it is just what you are looking for:
Selecting a Subfolder from a Particular Folder[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900