System.IO.Directory.GetAccessControl 方法 (String)

方法描述

获取一个 DirectorySecurity 对象,该对象封装指定目录的访问控制列表 (ACL) 项。

语法定义(C# System.IO.Directory.GetAccessControl 方法 (String) 的用法)

public static DirectorySecurity GetAccessControl(
	string path
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
path System-String 包含 DirectorySecurity 对象的目录的路径,该对象描述文件的访问控制列表 (ACL) 信息。
返回值 System.Security.AccessControl.DirectorySecurity 一个 DirectorySecurity 对象,它封装由 path 参数描述的文件的访问控制规则。

提示和注释

使用 GetAccessControl 方法检索某个目录的访问控制列表 (ACL) 项。

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

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

System.IO.Directory.GetAccessControl 方法 (String)例子

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

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

namespace FileSystemExample
{
    class DirectoryExample
    {
        public static void Main()
        {
            try
            {
                string DirectoryName = "TestDirectory";

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

                // Add the access control entry to the directory.
                AddDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

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

                // Remove the access control entry from the directory.
                RemoveDirectorySecurity(DirectoryName, @"MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow);

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

            Console.ReadLine();
        }

        // Adds an ACL entry on the specified directory for the specified account.
        public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the 
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

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

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);

        }

        // Removes an ACL entry on the specified directory for the specified account.
        public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            // Create a new DirectoryInfo object.
            DirectoryInfo dInfo = new DirectoryInfo(FileName);

            // Get a DirectorySecurity object that represents the 
            // current security settings.
            DirectorySecurity dSecurity = dInfo.GetAccessControl();

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

            // Set the new access settings.
            dInfo.SetAccessControl(dSecurity);

        }
    }
}

异常

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

命名空间

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 系统要求。