Get file Access Rules in CSharp

Description

The following code shows how to get file Access Rules.

Example


         //w  ww  . j  av  a2 s.co m

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.Security;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        FileSecurity acl = File.GetAccessControl(@"C:\Windows\");
        AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));
        foreach (AuthorizationRule rule in rules)
        {
            Console.WriteLine(rule.IdentityReference);
            FileSystemAccessRule accessRule = rule as FileSystemAccessRule;
            if (accessRule != null)
                Console.WriteLine("  ...{0}", accessRule.FileSystemRights);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip