System.IO.File.SetAccessControl 方法

方法描述

对指定的文件应用由 FileSecurity 对象描述的访问控制列表 (ACL) 项。

语法定义(C# System.IO.File.SetAccessControl 方法 的用法)

public static void SetAccessControl(
	string path,
	FileSecurity fileSecurity
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
path System-String 在其中添加或移除访问控制列表 (ACL) 项的文件。
fileSecurity System-Security-AccessControl-FileSecurity 一个 FileSecurity 对象,描述应用于由 path 参数描述的文件的 ACL 项。
返回值 void

提示和注释

SetAccessControl 方法将访问控制列表 (ACL) 项应用于表示非继承 ACL 列表的文件。

警告

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

ACL 描述对给定文件具有或没有执行特定操作的权限的个人和(或)组。 有关更多信息,请参见ACL 技术概述和如何:添加或移除访问控制列表项。

SetAccessControl 方法仅保持创建对象之后进行了修改的 FileSecurity 对象。如果 FileSecurity 对象尚未进行修改,它将不会保持在文件中。因此,无法从文件检索一个 FileSecurity 对象并将相同的对象重新应用于其他文件。

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

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

为目标文件创建一个新的 FileSecurity 对象。

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

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

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

在 NTFS 环境中,如果用户对父文件夹具有 ListDirectory 权限,则会向该用户授予 ReadAttributes 和 ReadExtendedAttributes 权限。 若要拒绝 ReadAttributes 和 ReadExtendedAttributes,请在父目录上拒绝 ListDirectory。

System.IO.File.SetAccessControl 方法例子

您必须提供有效的用户或组帐户才能运行此示例。

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

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "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 (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)
        {


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

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, 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)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = 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);

        }
    }
}

异常

异常 异常描述
IOException 打开文件时发生了 I/O 错误。
SEHException path 参数为 null。
SystemException 未能找到文件。
UnauthorizedAccessException
  • path 参数指定了一个只读文件。
  • 在当前平台上不支持此操作。
  • path 参数指定了一个目录。
  • 调用方没有所要求的权限。
ArgumentNullException fileSecurity 参数为 null。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。