FileSystemAccessRule 构造函数

定义

初始化 FileSystemAccessRule 类的新实例。

重载

FileSystemAccessRule(IdentityReference, FileSystemRights, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:对用户帐户的引用、指定与访问规则关联的操作的类型的值,以及指定是允许还是拒绝该操作的值。

FileSystemAccessRule(String, FileSystemRights, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:用户帐户的名称、指定与访问规则关联的操作的类型的值,以及描述是允许还是拒绝该操作的值。

FileSystemAccessRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:对用户帐户的引用、指定与访问规则关联的操作的类型的值、确定如何继承权限的值、确定如何传播权限的值,以及指定是允许还是拒绝该操作的值。

FileSystemAccessRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:用户帐户的名称、指定与访问规则关联的操作的类型的值、确定如何继承权限的值、确定如何传播权限的值,以及指定是允许还是拒绝该操作的值。

FileSystemAccessRule(IdentityReference, FileSystemRights, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:对用户帐户的引用、指定与访问规则关联的操作的类型的值,以及指定是允许还是拒绝该操作的值。

public:
 FileSystemAccessRule(System::Security::Principal::IdentityReference ^ identity, System::Security::AccessControl::FileSystemRights fileSystemRights, System::Security::AccessControl::AccessControlType type);
public FileSystemAccessRule (System.Security.Principal.IdentityReference identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType type);
new System.Security.AccessControl.FileSystemAccessRule : System.Security.Principal.IdentityReference * System.Security.AccessControl.FileSystemRights * System.Security.AccessControl.AccessControlType -> System.Security.AccessControl.FileSystemAccessRule
Public Sub New (identity As IdentityReference, fileSystemRights As FileSystemRights, type As AccessControlType)

参数

identity
IdentityReference

封装对用户帐户的引用的 IdentityReference 对象。

fileSystemRights
FileSystemRights

FileSystemRights 值之一,该值指定与访问规则关联的操作的类型。

type
AccessControlType

AccessControlType 值之一,该值指定是允许还是拒绝该操作。

例外

identity 参数不是一个 IdentityReference 对象。

identity 参数为 null

一个错误枚举被传递给 type 参数。

注解

使用此构造函数创建可以使用 或 DirectorySecurity 类持久保存FileSecurity的访问控制规则。 访问控制规则定义用户帐户权限,用于确定在运行 Windows Microsoft的计算机上允许或禁止哪些操作。

适用于

FileSystemAccessRule(String, FileSystemRights, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:用户帐户的名称、指定与访问规则关联的操作的类型的值,以及描述是允许还是拒绝该操作的值。

public:
 FileSystemAccessRule(System::String ^ identity, System::Security::AccessControl::FileSystemRights fileSystemRights, System::Security::AccessControl::AccessControlType type);
public FileSystemAccessRule (string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType type);
new System.Security.AccessControl.FileSystemAccessRule : string * System.Security.AccessControl.FileSystemRights * System.Security.AccessControl.AccessControlType -> System.Security.AccessControl.FileSystemAccessRule
Public Sub New (identity As String, fileSystemRights As FileSystemRights, type As AccessControlType)

参数

identity
String

用户帐户的名称。

fileSystemRights
FileSystemRights

FileSystemRights 值之一,该值指定与访问规则关联的操作的类型。

type
AccessControlType

AccessControlType 值之一,该值指定是允许还是拒绝该操作。

例外

identity 参数为 null

一个错误枚举被传递给 type 参数。

示例

下面的代码示例使用 FileSecurity 类 (ACE) 从文件中添加和删除访问控制项。 你必须提供有效的用户或组帐户以运行此示例。

using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;

// Adds an ACL entry on the specified file for the specified account.

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(gcnew 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.

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(gcnew FileSystemAccessRule
                                      (account,rights, controlType));

    // Set the new access settings.
    File::SetAccessControl(fileName, fSecurity);
}

int main()
{
    try
    {
        String^ fileName = "test.xml";

        Console::WriteLine("Adding access control entry for " + fileName);

        // Add the access control entry to the file.
        AddFileSecurity(fileName, "MYDOMAIN\\MyAccount", 
            FileSystemRights::ReadData, AccessControlType::Allow);

        Console::WriteLine("Removing access control entry from " + fileName);

        // Remove the access control entry from the file.
        RemoveFileSecurity(fileName, "MYDOMAIN\\MyAccount", 
            FileSystemRights::ReadData, AccessControlType::Allow);

        Console::WriteLine("Done.");
    }
    catch (Exception^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}
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);
        }
    }
}
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

注解

使用此构造函数创建可以使用 或 DirectorySecurity 类持久保存FileSecurity的访问控制规则。 访问控制规则定义用户帐户权限,用于确定在运行 Windows Microsoft的计算机上允许或禁止哪些操作。

参数 identity 必须标识当前计算机或域上的有效帐户。 字符串采用以下形式,其中 DOMAIN 是有效域或计算机名称的名称,是 account 域或计算机上的有效用户帐户的名称: DOMAIN\account

适用于

FileSystemAccessRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:对用户帐户的引用、指定与访问规则关联的操作的类型的值、确定如何继承权限的值、确定如何传播权限的值,以及指定是允许还是拒绝该操作的值。

public:
 FileSystemAccessRule(System::Security::Principal::IdentityReference ^ identity, System::Security::AccessControl::FileSystemRights fileSystemRights, System::Security::AccessControl::InheritanceFlags inheritanceFlags, System::Security::AccessControl::PropagationFlags propagationFlags, System::Security::AccessControl::AccessControlType type);
public FileSystemAccessRule (System.Security.Principal.IdentityReference identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propagationFlags, System.Security.AccessControl.AccessControlType type);
new System.Security.AccessControl.FileSystemAccessRule : System.Security.Principal.IdentityReference * System.Security.AccessControl.FileSystemRights * System.Security.AccessControl.InheritanceFlags * System.Security.AccessControl.PropagationFlags * System.Security.AccessControl.AccessControlType -> System.Security.AccessControl.FileSystemAccessRule
Public Sub New (identity As IdentityReference, fileSystemRights As FileSystemRights, inheritanceFlags As InheritanceFlags, propagationFlags As PropagationFlags, type As AccessControlType)

参数

identity
IdentityReference

封装对用户帐户的引用的 IdentityReference 对象。

fileSystemRights
FileSystemRights

FileSystemRights 值之一,该值指定与访问规则关联的操作的类型。

inheritanceFlags
InheritanceFlags

InheritanceFlags 值之一,该值指定访问掩码如何传播到子对象。

propagationFlags
PropagationFlags

PropagationFlags 值之一,该值指定访问控制项 (ACE) 如何传播到子对象。

type
AccessControlType

AccessControlType 值之一,该值指定是允许还是拒绝该操作。

例外

identity 参数不是一个 IdentityReference 对象。

identity 参数为 null

一个错误枚举被传递给 type 参数。

- 或 -

一个错误枚举被传递给 inheritanceFlags 参数。

- 或 -

一个错误枚举被传递给 propagationFlags 参数。

注解

使用此构造函数创建可以使用 或 DirectorySecurity 类持久保存FileSecurity的访问控制规则。 访问控制规则定义用户帐户权限,用于确定在运行 Windows Microsoft的计算机上允许或禁止哪些操作。

适用于

FileSystemAccessRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

使用以下内容初始化 FileSystemAccessRule 类的新实例:用户帐户的名称、指定与访问规则关联的操作的类型的值、确定如何继承权限的值、确定如何传播权限的值,以及指定是允许还是拒绝该操作的值。

public:
 FileSystemAccessRule(System::String ^ identity, System::Security::AccessControl::FileSystemRights fileSystemRights, System::Security::AccessControl::InheritanceFlags inheritanceFlags, System::Security::AccessControl::PropagationFlags propagationFlags, System::Security::AccessControl::AccessControlType type);
public FileSystemAccessRule (string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propagationFlags, System.Security.AccessControl.AccessControlType type);
new System.Security.AccessControl.FileSystemAccessRule : string * System.Security.AccessControl.FileSystemRights * System.Security.AccessControl.InheritanceFlags * System.Security.AccessControl.PropagationFlags * System.Security.AccessControl.AccessControlType -> System.Security.AccessControl.FileSystemAccessRule
Public Sub New (identity As String, fileSystemRights As FileSystemRights, inheritanceFlags As InheritanceFlags, propagationFlags As PropagationFlags, type As AccessControlType)

参数

identity
String

用户帐户的名称。

fileSystemRights
FileSystemRights

FileSystemRights 值之一,该值指定与访问规则关联的操作的类型。

inheritanceFlags
InheritanceFlags

InheritanceFlags 值之一,该值指定访问掩码如何传播到子对象。

propagationFlags
PropagationFlags

PropagationFlags 值之一,该值指定访问控制项 (ACE) 如何传播到子对象。

type
AccessControlType

AccessControlType 值之一,该值指定是允许还是拒绝该操作。

例外

identity 参数为 null

一个错误枚举被传递给 type 参数。

- 或 -

一个错误枚举被传递给 inheritanceFlags 参数。

- 或 -

一个错误枚举被传递给 propagationFlags 参数。

注解

使用此构造函数创建可以使用 或 DirectorySecurity 类持久保存FileSecurity的访问控制规则。 访问控制规则定义用户帐户权限,用于确定在运行 Windows Microsoft的计算机上允许或禁止哪些操作。

参数 identity 必须标识当前计算机或域上的有效帐户。 字符串采用以下形式,其中 DOMAIN 是有效域或计算机名称的名称,是 account 域或计算机上的有效用户帐户的名称: DOMAIN\account

适用于