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

I have the below code that restrict access on the following folders:
ClientData and

But when the app gets to this line of code: directoryInfo.SetAccessControl(dirSecurity);
it hangs, and I can't figure it out why. I just want to know what could be the cause of this or maybe I'm doing something wrong there please help.

private static void SetFolderPermission(DirectoryInfo directoryInfo)
    var dirSecurity = directoryInfo.GetAccessControl();
    dirSecurity.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name,

private static void RestrictFolderWritablePermission()
  var folderNames = AppUtil.GetClientDataFolder(AppUtil.ResourceType.Registers).Replace('\\', ',').Split(',').ToArray();
  foreach (var folderName in folderNames)
      if (!folderName.IsNullOrEmpty())
          switch (folderName)
              case "VSmart3":
                  var vSmart3Folder = AppUtil.GetClientDataFolder(AppUtil.ResourceType.Registers).Substring(0, 27);
                  SetFolderPermission(new DirectoryInfo(vSmart3Folder));
              case "ClientData":
                  var clientDataFolder = AppUtil.GetClientDataFolder(AppUtil.ResourceType.Registers).Substring(0, 38);
                  SetFolderPermission(new DirectoryInfo(clientDataFolder));
              case "Registers":
                  var registerFolder = AppUtil.GetClientDataFolder(AppUtil.ResourceType.Registers).Substring(0, 48);
                  SetFolderPermission(new DirectoryInfo(registerFolder));


Your help is much appreciated

Kindest Regards
Lucky Khoza
Updated 4-Feb-16 2:01am
Dave Kreskowiak 4-Feb-16 10:06am    
Just a thought. I think the code is not really hanging but is taking forever to execute. Are you sure the changes you're making are NOT being applied to all the child objects in the target folder?
Sergey Alexandrovich Kryukov 4-Feb-16 14:08pm    
"Hanging" is not a strict or well-defined term. "Taking forever to execute" is quite commonly called "hanging"...
Dave Kreskowiak 5-Feb-16 9:51am    
I know that. But the common definition of "hang" is code that just sits there and does nothing forever. I don't think that's the case with this code.
Sergey Alexandrovich Kryukov 4-Feb-16 14:11pm    
Just use the debugger. In Visual Studio, when things "hang", it will be good to click Debug / Break All...
You will see...

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