FileSystemAccessRule 類別

定義

表示為檔案或目錄定義存取規則之存取控制項目 (ACE) 的抽象。Represents an abstraction of an access control entry (ACE) that defines an access rule for a file or directory. 這個類別無法被繼承。This class cannot be inherited.

public ref class FileSystemAccessRule sealed : System::Security::AccessControl::AccessRule
[System.Security.SecurityCritical]
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
type FileSystemAccessRule = class
    inherit AccessRule
Public NotInheritable Class FileSystemAccessRule
Inherits AccessRule
繼承
FileSystemAccessRule
屬性

範例

下列程式碼範例會使用FileSecurity類別, 在檔案中加入並移除存取控制專案 (ACE)。The following code example uses the FileSecurity class to add and then remove an access control entry (ACE) from a file. 您必須提供有效的使用者或群組帳戶,才能執行這個範例。You must supply a valid user or group account to run this example.

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

備註

FileSystemAccessRule類別代表基礎存取控制專案 (ACE) 的抽象概念, 指定使用者帳戶、提供的存取類型 (讀取、寫入等等), 以及是否要允許或拒絕該許可權。The FileSystemAccessRule class represents an abstraction of an underlying access control entry (ACE) that specifies a user account, the type of access to provide (read, write, and so on), and whether to allow or deny that right. 這個類別也可以指定存取規則如何傳播到子物件。This class can also specify how access rules are propagated to child objects.

FileSystemAccessRule使用類別建立新的存取規則。Use the FileSystemAccessRule class to create a new access rule. 您可以使用FileSecurityDirectorySecurity類別保存規則。You can persist the rule using the FileSecurity or DirectorySecurity class.

建構函式

FileSystemAccessRule(IdentityReference, FileSystemRights, AccessControlType)

使用對使用者帳戶的參考、指定與存取規則關聯之作業類型的值,以及指定允許還是拒絕作業的值,初始化 FileSystemAccessRule 類別的新執行個體。Initializes a new instance of the FileSystemAccessRule class using a reference to a user account, a value that specifies the type of operation associated with the access rule, and a value that specifies whether to allow or deny the operation.

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

使用對使用者帳戶的參考、指定與存取規則關聯之作業類型的值、判斷如何繼承權限的值、判斷如何散佈權限的值,以及指定允許還是拒絕作業的值,初始化 FileSystemAccessRule 類別的新執行個體。Initializes a new instance of the FileSystemAccessRule class using a reference to a user account, a value that specifies the type of operation associated with the access rule, a value that determines how rights are inherited, a value that determines how rights are propagated, and a value that specifies whether to allow or deny the operation.

FileSystemAccessRule(String, FileSystemRights, AccessControlType)

利用使用者帳戶的名稱、指定與存取規則關聯之作業類型的值,以及描述允許還是拒絕作業的值,初始化 FileSystemAccessRule 類別的新執行個體。Initializes a new instance of the FileSystemAccessRule class using the name of a user account, a value that specifies the type of operation associated with the access rule, and a value that describes whether to allow or deny the operation.

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

利用使用者帳戶的名稱、指定與存取規則關聯之作業類型的值、判斷如何繼承權限的值、判斷如何散佈權限的值,以及指定允許還是拒絕作業的值,初始化 FileSystemAccessRule 類別的新執行個體。Initializes a new instance of the FileSystemAccessRule class using the name of a user account, a value that specifies the type of operation associated with the access rule, a value that determines how rights are inherited, a value that determines how rights are propagated, and a value that specifies whether to allow or deny the operation.

屬性

AccessControlType

取得與這個 AccessControlType 物件相關聯的 AccessRule 值。Gets the AccessControlType value associated with this AccessRule object.

(繼承來源 AccessRule)
AccessMask

取得此規則的存取遮罩。Gets the access mask for this rule.

(繼承來源 AuthorizationRule)
FileSystemRights

取得與目前 FileSystemRights 物件關聯的 FileSystemAccessRule 旗標。Gets the FileSystemRights flags associated with the current FileSystemAccessRule object.

IdentityReference

取得要套用此規則的 IdentityReferenceGets the IdentityReference to which this rule applies.

(繼承來源 AuthorizationRule)
InheritanceFlags

取得旗標值,這個值會決定子物件繼承這項規則的方式。Gets the value of flags that determine how this rule is inherited by child objects.

(繼承來源 AuthorizationRule)
IsInherited

取得值,表示這個規則是否會明確地設定或繼承自父容器物件。Gets a value indicating whether this rule is explicitly set or is inherited from a parent container object.

(繼承來源 AuthorizationRule)
PropagationFlags

取得傳播旗標的值,該值判斷這個規則的繼承如何傳播到子物件。Gets the value of the propagation flags, which determine how inheritance of this rule is propagated to child objects. 只有當 InheritanceFlags 列舉的值不是 None,這個屬性才是重要的。This property is significant only when the value of the InheritanceFlags enumeration is not None.

(繼承來源 AuthorizationRule)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於