SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Manikavelu Velayutham Articles | C# Language February 09, 2011
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.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Very useful article manikavelu.

Posted by Dinesh Beniwal Feb 09, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor