C# File SetAccessControl

Description

File SetAccessControl Applies access control list (ACL) entries described by a FileSecurity object to the specified file.

Syntax

File.SetAccessControl has the following syntax.


public static void SetAccessControl(
  string path,
  FileSecurity fileSecurity
)

Parameters

File.SetAccessControl has the following parameters.

  • path - A file to add or remove access control list (ACL) entries from.
  • fileSecurity - A FileSecurity object that describes an ACL entry to apply to the file described by the path parameter.

Returns

File.SetAccessControl method returns

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  w w. jav 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