Export (0) Print
Expand All

FileInfo.SetAccessControl Method

Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileInfo object.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public void SetAccessControl(
	FileSecurity fileSecurity
)

Parameters

fileSecurity
Type: System.Security.AccessControl.FileSecurity

A FileSecurity object that describes an access control list (ACL) entry to apply to the current file.

ExceptionCondition
ArgumentNullException

The fileSecurity parameter is null.

SystemException

The file could not be found or modified.

UnauthorizedAccessException

The current process does not have access to open the file.

PlatformNotSupportedException

The current operating system is not Microsoft Windows 2000 or later.

The SetAccessControl method applies access control list (ACL) entries to the current file that represents the noninherited ACL list.

Use the SetAccessControl method whenever you need to add or remove ACL entries from a file.

Caution noteCaution

The ACL specified for the fileSecurity parameter replaces the existing ACL for the file. To add permissions for a new user, use the GetAccessControl method to obtain the existing ACL, modify it, and then use SetAccessControl to apply it back to the file.

An ACL describes individuals and/or groups who have, or do not have, rights to specific actions on the given file. For more information, see ACL Technology Overview and How to: Add or Remove Access Control List Entries.

The SetAccessControl method persists only FileSecurity objects that have been modified after object creation.  If a FileSecurity object has not been modified, it will not be persisted to a file.  Therefore, it is not possible to retrieve a FileSecurity object from one file and reapply the same object to another file.

To copy ACL information from one file to another:

  1. Use the GetAccessControl method to retrieve the FileSecurity object from the source file.

  2. Create a new FileSecurity object for the destination file.

  3. Use the GetSecurityDescriptorBinaryForm or GetSecurityDescriptorSddlForm method of the source FileSecurity object to retrieve the ACL information.

  4. Use the SetSecurityDescriptorBinaryForm or SetSecurityDescriptorSddlForm method to copy the information retrieved in step 3 to the destination FileSecurity object.

  5. Set the destination FileSecurity object to the destination file using the SetAccessControl method.

The following code example uses the GetAccessControl method and the SetAccessControl method to add and then remove an ACL entry from a file. You must supply a valid user or group account to run this example.

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string FileName = "c:/test.xml";

                Console.WriteLine("Adding access control entry for " + FileName);

                // Add the access control entry to the file. 
                // Before compiling this snippet, change MyDomain to your  
                // domain name and MyAccessAccount to the name  
                // you use to access your domain.
                AddFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from " + FileName);

                // Remove the access control entry from the file. 
                // Before compiling this snippet, change MyDomain to your  
                // domain name and MyAccessAccount to the name  
                // you use to access your domain.
                RemoveFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

        }

        // Adds an ACL entry on the specified file for the specified account. 
        public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);

            // Get a FileSecurity object that represents the  
            // current security settings.
            FileSecurity fSecurity = fInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            fInfo.SetAccessControl(fSecurity);

        }

        // Removes an ACL entry on the specified file for the specified account. 
        public static void RemoveFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);

            // Get a FileSecurity object that represents the  
            // current security settings.
            FileSecurity fSecurity = fInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                            Rights,
                                                            ControlType));

            // Set the new access settings.
            fInfo.SetAccessControl(fSecurity);

        }
    }
}
//This code produces output similar to the following;  
//results may vary based on the computer/file structure/etc.: 
// 
//Adding access control entry for c:\test.xml 
//Removing access control entry from c:\test.xml 
//Done. 
//

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft