语言

FileInfo.SetAccessControl(FileSecurity) 方法

定义

将对象描述 FileSecurity 的访问控制列表(ACL)条目应用于当前 FileInfo 对象描述的文件。

public:
 void SetAccessControl(System::Security::AccessControl::FileSecurity ^ fileSecurity);
public void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity);
member this.SetAccessControl : System.Security.AccessControl.FileSecurity -> unit
Public Sub SetAccessControl (fileSecurity As FileSecurity)

参数

fileSecurity
FileSecurity

描述 FileSecurity 要应用于当前文件的访问控制列表(ACL)条目的对象。

例外

参数 fileSecurity 为 null.

找不到或修改该文件。

当前进程无权打开该文件。

示例

下面的代码示例使用 GetAccessControl 该方法和 SetAccessControl 方法添加 ACL 条目,然后从文件中删除 ACL 条目。 必须提供有效的用户或组帐户才能运行此示例。

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(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(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.
//
Imports System.IO
Imports System.Security.AccessControl



Module FileExample

    Sub Main()
        Try
            Dim FileName As String = "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 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)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)

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

        ' Add the FileSystemAccessRule to the security settings. 
        fSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))

        ' Set the new access settings.
        fInfo.SetAccessControl(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)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(FileName)

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

        ' Add the FileSystemAccessRule to the security settings. 
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))

        ' Set the new access settings.
        fInfo.SetAccessControl(fSecurity)

    End Sub
End Module
'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.
'

注解

此方法 SetAccessControl 将访问控制列表 (ACL) 条目应用于表示非未加入的 ACL 列表的当前文件。

SetAccessControl每当需要添加或删除文件中的 ACL 条目时,请使用该方法。

Caution

为 fileSecurity 参数指定的 ACL 将替换文件的现有 ACL。 若要为新用户添加权限,请使用 GetAccessControl 该方法获取现有 ACL、修改它,然后使用 SetAccessControl 该方法将其应用回文件。

ACL 描述对给定文件执行特定操作或无权执行的人员和组。 有关详细信息,请参阅 “如何:添加或删除访问控制列表条目”。

该方法 SetAccessControl 仅 FileSecurity 保留创建对象后已修改的对象。 FileSecurity如果对象尚未修改,则不会将其保存到文件中。 因此,无法从一个文件中检索对象 FileSecurity ,并将同一对象重新应用到另一个文件。

将 ACL 信息从一个文件复制到另一个文件:

  1. GetAccessControl使用该方法从源文件中检索FileSecurity对象。

  2. 为目标文件创建新 FileSecurity 对象。

  3. GetSecurityDescriptorBinaryForm使用源GetSecurityDescriptorSddlForm对象的或FileSecurity方法检索 ACL 信息。

  4. SetSecurityDescriptorBinaryForm使用或SetSecurityDescriptorSddlForm方法将步骤 3 中检索到的信息复制到目标FileSecurity对象。

  5. 使用FileSecurity该方法将目标SetAccessControl对象设置为目标文件。

适用于