C# File GetAccessControl(String)

Description

File GetAccessControl(String) Gets a FileSecurity object that encapsulates the access control list (ACL) entries for a specified file.

Syntax

File.GetAccessControl(String) has the following syntax.


public static FileSecurity GetAccessControl(
  string path
)

Parameters

File.GetAccessControl(String) has the following parameters.

  • path - The path to a file containing a FileSecurity object that describes the file's access control list (ACL) information.

Returns

File.GetAccessControl(String) method returns A FileSecurity object that encapsulates the access control rules for the file described by the path parameter.

Example

The following code example uses the GetAccessControl and the SetAccessControl methods to add and then remove an access control list (ACL) entry from a file.


using System;// w  ww  .j av  a  2 s .com
using System.IO;
using System.Security.AccessControl;

class FileExample
{
    public static void Main()
    {
        string fileName = "test.xml";
        FileSecurity fSecurity = File.GetAccessControl(fileName);
        fSecurity.AddAccessRule(new FileSystemAccessRule(@"DomainName\AccountName",
                FileSystemRights.ReadData, AccessControlType.Allow));
        File.SetAccessControl(fileName, fSecurity);

        fSecurity.RemoveAccessRule(new FileSystemAccessRule(@"DomainName\AccountName",
                FileSystemRights.ReadData, AccessControlType.Allow));

        File.SetAccessControl(fileName, fSecurity);

    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter