Adding Groups / User names and Permissions for a Directory in C#


This article shows you the procedure to create a directory, adding a specific Group or the User Name account for that directory and providing the required permission for the same. Normally the above process can be easily done through manual procedure. Here we are going to implement the same programmatically through C#.

Namespaces Required

using System.IO;
using System.Security.AccessControl;

Actual Implementation

CreateDirectory(@"D:\DirectoryName", @"DomainName\UserName");

CreateDirectory(@"D:\DirectoryName", @"DomainName\UserName");

public void CreateDirectory(string DirectoryName, string UserAccount)
{
    if (!Directory.Exists(DirectoryName))
        Directory.CreateDirectory(DirectoryName);
    // Calls the another function to add users and permissions
    AddUsersAndPermissions(DirectoryName, UserAccount, FileSystemRights.FullControl,AccessControlType.Allow);
}

public void AddUsersAndPermissions(string DirectoryName, string UserAccount, FileSystemRights UserRights, AccessControlType AccessType)
{
    // Create a DirectoryInfo object.
    DirectoryInfo directoryInfo = new DirectoryInfo(DirectoryName);
 
    // Get security settings.
    DirectorySecurity dirSecurity = directoryInfo.GetAccessControl();

    // Add the FileSystemAccessRule to the security settings.
    dirSecurity.AddAccessRule(new FileSystemAccessRule(UserAccount, UserRights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessType));

    // Set the access settings.
    directoryInfo.SetAccessControl(dirSecurity);
}

How to give access to folders, subfolders and files in it?

Below list will shows you on which combination of flags, what access are given to the folders. Generally InheritanceFlags and PropagationFlags are the two flags play a vital role in providing the access.

  1. Subfolders and Files only:

    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
    PropagationFlags.InheritOnly
     
  2. This Folder, Subfolders and Files:

    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
    PropagationFlags.None
     
  3. This Folder, Subfolders and Files:

    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
    PropagationFlags.NoPropagateInherit
     
  4. This folder and subfolders:

    InheritanceFlags.ContainerInherit,
    PropagationFlags.None
     
  5. Subfolders only:

    InheritanceFlags.ContainerInherit,
    PropagationFlags.InheritOnly
     
  6. This folder and files:

    InheritanceFlags.ObjectInherit,
    PropagationFlags.None
     
  7. This folder and files:

    InheritanceFlags.ObjectInherit,
    PropagationFlags.NoPropagateInherit

The above combinations can be applied in the code based on your requirements. That's the simple way to programmatically create the directory, adding the groups, users and providing the permissions for them.

Up Next
    Ebook Download
    View all
    Learn
    View all