How to set folder permission using C# code?


Not every system has the right folder permission set to all user account and hence, sometime we need special access to specific folder durin... - Article authored by Kunal Chowdhury on .

Not every system has the right folder permission set to all user account and hence, sometime we need special access to specific folder during installation or while executing the application. It is not possible to manually set it up by browsing the folder and hence an automation is mandatory.

 

If you are looking for a way to set the right permission to specific folder(s) using C# code, this post will help you. Continue reading to learn more about it and grab the code.

 

How to set folder permission using C# code? (www.kunal-chowdhury.com)

 

If you want to set folder permission programmatically, before going further you should first collect couple of data. First one is the complete folder path, the identity of the access rule for which you want to set the permission, the file system rights (read, write, full control etc.), whether you want to inherit folder permission.

 

Once you are ready with all these details, get the handle of the directory by creating an object of DirectoryInfo, passing the complete folder path. While setting the correct file system access rule, set the user identity under which you want to set the special permission along with other details like read/write/fullControl access etc.

 

Below is the code snippet that you can refer and modify accordingly as per your business need:

 

 

public static void SetFolderPermission(string folderPath)
{
    var directoryInfo = new DirectoryInfo(folderPath);
    var directorySecurity = directoryInfo.GetAccessControl();
    var currentUserIdentity = WindowsIdentity.GetCurrent();
    var fileSystemRule = new FileSystemAccessRule(currentUserIdentity.Name, 
                                                  FileSystemRights.Read, 
                                                  InheritanceFlags.ObjectInherit |
                                                  InheritanceFlags.ContainerInherit, 
                                                  PropagationFlags.None,
                                                  AccessControlType.Allow);
          
    directorySecurity.AddAccessRule(fileSystemRule);
    directoryInfo.SetAccessControl(directorySecurity);
}

 

I hope, this will be helpful for you in case you are looking for an automated way to set specific folder permission to specific user by using C# code. You can further study it’s different parameters and fine tune it as per your requirement. Let me know, if you need further details on it. I will be happy to help you.

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.