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 to Create Directory
public void CreateDirectory(string DirectoryName, string UserAccount)
{
if (!Directory.Exists(DirectoryName))
Directory.CreateDirectory(DirectoryName);
// Calls 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);
}
// Usage: CreateDirectory(@"D:\DirectoryName", @"DomainName\UserName");
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.
- Subfolders and Files only: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit PropagationFlags.InheritOnly
- This Folder, Subfolders and Files: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit PropagationFlags.None
- This Folder, Subfolders and Files: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.NoPropagateInherit
- This folder and subfolders: InheritanceFlags.ContainerInherit, PropagationFlags.None
- Subfolders only: InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly
- This folder and files: InheritanceFlags.ObjectInherit, PropagationFlags.None
- 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.

AshuPosted May 9, 2014, 1:36 AM
Very nice code Thank you.
SayanPosted Dec 3, 2013, 12:13 AM
attempted to perform an unauthorized operation error
mahendra bishtPosted Aug 31, 2013, 2:04 AM
nice article. Setting ACE is very slow for folder with many files and subfolders. My folder has 35602 files and 648 subfolders and SetAccessControl method needs 30 minute to remove a user from this folder.
mahendra bishtPosted Aug 31, 2013, 2:04 AM
nice article. I am struggling to resolve a question.
Sumit KumawatPosted Apr 3, 2013, 7:40 AM
Hi Manikavelu , I am creating directory and placing my DB inside the directory using installer class while deploying my .exe . which will create appropriate directory in AppData folder('C' drive) of user's Account and also giving permission by using your code. but while accessing the DB through my setup,I am not able to access my DB. it is giving me following error "Access to the database file is not allowed. [ File name = UserRecord.sdf ] " Please help
Dinesh BeniwalPosted Feb 9, 2011, 5:37 AM
Very useful article manikavelu.