Export (0) Print
Expand All

File.SetAccessControl Method

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

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

'Declaration
Public Shared Sub SetAccessControl ( _
	path As String, _
	fileSecurity As FileSecurity _
)

Parameters

path
Type: System.String

A file to add or remove access control list (ACL) entries from.

fileSecurity
Type: System.Security.AccessControl.FileSecurity

A FileSecurity object that describes an ACL entry to apply to the file described by the path parameter.

ExceptionCondition
IOException

An I/O error occurred while opening the file.

SEHException

The path parameter is Nothing.

SystemException

The file could not be found.

UnauthorizedAccessException

The path parameter specified a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

The path parameter specified a directory.

-or-

The caller does not have the required permission.

ArgumentNullException

The fileSecurity parameter is Nothing.

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

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.

In NTFS environments, ReadAttributes and ReadExtendedAttributes are granted to the user if the user has ListDirectory rights on the parent folder. To deny ReadAttributes and ReadExtendedAttributes, deny ListDirectory on the parent directory.

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

Imports System
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try 
            Dim fileName As String = "test.xml"

            Console.WriteLine("Adding access control entry for " & fileName)

            ' Add the access control entry to the file.
            AddFileSecurity(fileName, "DomainName\AccountName", _
                FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Removing access control entry from " & fileName)

            ' Remove the access control entry from the file.
            RemoveFileSecurity(fileName, "DomainName\AccountName", _
                FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Done.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try 

    End Sub 


    ' Adds an ACL entry on the specified file for the specified account. 
    Sub AddFileSecurity(ByVal fileName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

        ' Get a FileSecurity object that represents the  
        ' current security settings. 
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

        ' Add the FileSystemAccessRule to the security settings.  
        Dim accessRule As FileSystemAccessRule = _
            New FileSystemAccessRule(account, rights, controlType)

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)

    End Sub 


    ' Removes an ACL entry on the specified file for the specified account. 
    Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

        ' Get a FileSecurity object that represents the  
        ' current security settings. 
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

        ' Remove the FileSystemAccessRule from the security settings. 
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(account, _
            rights, controlType))

        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)

    End Sub 
End Module

.NET Framework

Supported in: 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