Friday, February 4, 2011

Setting NTFS permissions for Directories and Files C#

It's easy setting NTFS permissions in .NET.  There are a couple different flags one has to be weary about, but once you figure them out you realise the full power of the Library.  Next is code to allow Full/Everyone/CI/OI Inheritence for all objects.

Here is the code:

using System.Security.AccessControl;

public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        // Create a new DirectoryInfo object.
        DirectoryInfo dInfo = new DirectoryInfo(FileName);

        // Get a DirectorySecurity object that represents the 
        // current security settings.
        DirectorySecurity dSecurity = dInfo.GetAccessControl();

        // Add the FileSystemAccessRule to the security settings. 
        dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
        Rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None,

        // Set the new access settings.


No comments:

Post a Comment