FileSystemSecurity.AddAccessRule(FileSystemAccessRule) FileSystemSecurity.AddAccessRule(FileSystemAccessRule) FileSystemSecurity.AddAccessRule(FileSystemAccessRule) FileSystemSecurity.AddAccessRule(FileSystemAccessRule) Method

定義

將指定的存取控制清單 (ACL) 使用權限加入至目前的檔案或目錄。Adds the specified access control list (ACL) permission to the current file or directory.

public:
 void AddAccessRule(System::Security::AccessControl::FileSystemAccessRule ^ rule);
public void AddAccessRule (System.Security.AccessControl.FileSystemAccessRule rule);
override this.AddAccessRule : System.Security.AccessControl.FileSystemAccessRule -> unit
Public Sub AddAccessRule (rule As FileSystemAccessRule)

參數

rule
FileSystemAccessRule FileSystemAccessRule FileSystemAccessRule FileSystemAccessRule

FileSystemAccessRule 物件,表示要加入至檔案或目錄的存取控制清單 (ACL) 使用權限。A FileSystemAccessRule object that represents an access control list (ACL) permission to add to a file or directory.

例外狀況

範例

下列程式碼範例會使用FileSecurity類別, 在檔案中加入並移除存取控制清單 (ACL) 專案。The following code example uses the FileSecurity class to add and then remove an access control list (ACL) entry 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

備註

方法會將新的規則加入至包含FileSystemSecurity在物件中的規則清單。 AddAccessRuleThe AddAccessRule method adds a new rule to the list of rules contained within a FileSystemSecurity object.

如果指定的規則已經有存取控制清單 (ACL), 此AddAccessRule方法仍會加入規則, 但有一個例外狀況FileSystemAccessRule : 使用Deny列舉值建立的物件會取代使用所建立的物件。Allow列舉值。If an access control list (ACL) already exists for the specified rule, the AddAccessRule method will still add the rule, with one exception: a FileSystemAccessRule object created using the Deny enumeration value supersedes an object created using the Allow enumeration value.

若要將新的或變更的 ACL 資訊保存至檔案SetAccessControl , SetAccessControl請使用或方法。To persist new or changed ACL information to a file, use the SetAccessControl or SetAccessControl method. 若要將新的或變更的 ACL 資訊保存至目錄SetAccessControl , SetAccessControl請使用或方法。To persist new or changed ACL information to a directory, use the SetAccessControl or SetAccessControl method.

若要從檔案取出 ACL 資訊, 請使用GetAccessControlGetAccessControl方法。To retrieve ACL information from a file, use the GetAccessControl or GetAccessControl method. 若要從目錄取出 ACL 資訊, 請使用GetAccessControlGetAccessControl方法。To retrieve ACL information from a directory, use the GetAccessControl or GetAccessControl method.

當您新增存取規則但未設定Synchronize旗標時, 旗標會自動新增到您的Synchronize規則。When you add an access rule without setting the Synchronize flag, the Synchronize flag will be automatically added to your rule. 如果您稍後不指定Synchronize旗標而移除規則, 則會自動移除旗標。If you remove the rule later without specifying the Synchronize flag, the flag will automatically be removed.

適用於