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,
        ControlType));


        // Set the new access settings.
        dInfo.SetAccessControl(dSecurity);


    }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...