FileSecurity Class : FileSecurity « File Stream « C# / C Sharp






FileSecurity Class

 

using System;
using System.IO;
using System.Security.AccessControl;
class FileExample
{
    public static void Main()
    {
        string fileName = "test.xml";
        AddFileSecurity(fileName, @"DomainName\AccountName", FileSystemRights.ReadData, AccessControlType.Allow);

        RemoveFileSecurity(fileName, @"DomainName\AccountName", FileSystemRights.ReadData, AccessControlType.Allow);

    }
    public static void AddFileSecurity(string fileName, string account,
        FileSystemRights rights, AccessControlType controlType)
    {
        FileSecurity fSecurity = File.GetAccessControl(fileName);
        fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));
        File.SetAccessControl(fileName, fSecurity);
    }
    public static void RemoveFileSecurity(string fileName, string account, FileSystemRights rights, AccessControlType controlType)
    {
        FileSecurity fSecurity = File.GetAccessControl(fileName);
        fSecurity.RemoveAccessRule(new FileSystemAccessRule(account, rights, controlType));
        File.SetAccessControl(fileName, fSecurity);

    }
}

   
  








Related examples in the same category