如何限制用户选择特定文件夹 [英] How to restrict a user to select a specific folder

查看:98
本文介绍了如何限制用户选择特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用以下代码启动文件夹对话框,用户可以选择任何文件夹。



Hi,

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

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 .

        }
   }



但是现在新的要求出现了,并指出不允许用户选择特定文件夹。



例如如果用户试图选择名为PGA的文件夹,则不允许用户选择PGA文件夹。


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.

推荐答案

您可以进行任何所需的验证。在大多数情况下,这种要求并没有多大意义。



如果您的应用程序使用某些文件夹,用户不应保存自己的文件,那么最好使用应用程序数据的子文件夹。如果应该使用单个文件夹,那么为什么要询问用户。如果您只想要几个特定位置,那么最好只显示预定义位置列表...



或者,设置文件夹权限可能有用你想保护文件夹免受意外的用户更改,但我不会那么远。



请注意,用户可以随时手动移动文件,因此可能不值得投入大量时间进行这样的验证,因为一旦保存就可以通过复制文件轻松绕过它。



还限制到不包含PGA的文件夹可能过于严格。也许一个用户会拥有这些首字母并将其用作其用户名......或者可能有一个文件夹使用该名称来表示与您的应用程序无关的内容。



如果您仍想进行限制,我建议您只阻止特定的完整路径名,而不是包含某些文本的任意路径。
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.


在这里查看CodeProject上的这篇文章。我认为这正是你要找的:

从特定文件夹中选择子文件夹 [ ^ ]



祝你好运!
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!


这篇关于如何限制用户选择特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆