FileSecurity 類別

定義

表示檔案的存取控制和稽核安全性。 此類別無法獲得繼承。

public ref class FileSecurity sealed : System::Security::AccessControl::FileSystemSecurity
public sealed class FileSecurity : System.Security.AccessControl.FileSystemSecurity
[System.Security.SecurityCritical]
public sealed class FileSecurity : System.Security.AccessControl.FileSystemSecurity
type FileSecurity = class
    inherit FileSystemSecurity
[<System.Security.SecurityCritical>]
type FileSecurity = class
    inherit FileSystemSecurity
Public NotInheritable Class FileSecurity
Inherits FileSystemSecurity
繼承
屬性

範例

下列程式碼範例會 FileSecurity 使用 類別,從檔案中新增和移除存取控制清單 (ACL) 專案。 您必須提供有效的使用者或群組帳戶,才能執行這個範例。

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

備註

類別 FileSecurity 會指定系統檔案的存取權限,以及如何稽核存取嘗試。 這個類別會將存取權和稽核許可權表示為一組規則。 每個存取規則都是由 FileSystemAccessRule 物件表示,而每個稽核規則則由 FileSystemAuditRule 物件表示。

類別 FileSecurity 是基礎Microsoft Windows 檔案系統的抽象概念。 在此系統中,每個檔案都有一個選擇性存取控制清單 (DACL) ,可控制檔案的存取權,而系統存取控制清單 (SACL) ,這會指定稽核的存取控制嘗試。 FileSystemAccessRuleFileSystemAuditRule 類別是組成 DACL 和 SCL 的 ACE (ACE) 存取控制專案的抽象概念。

類別 FileSecurity 會隱藏 DACL 和 SCL 的許多詳細資料;您不必擔心 ACE 排序或 Null DACLS。

FileSecurity使用 類別來擷取、新增或變更代表檔案 DACL 和 SACL 的存取規則。

若要將新的或變更的存取或稽核規則保存到檔案,請使用 SetAccessControl 方法。 若要從現有的檔案擷取存取或稽核規則,請使用 GetAccessControl 方法。

建構函式

FileSecurity()

初始化 FileSecurity 類別的新執行個體。

FileSecurity(String, AccessControlSections)

使用 FileSecurity 列舉型別 (Enumeration) 之指定的值,從指定的檔案初始化 AccessControlSections 類別的新執行個體。

屬性

AccessRightType

取得 FileSystemSecurity 類別用來表示存取權限的列舉型別 (Enumeration)。

(繼承來源 FileSystemSecurity)
AccessRulesModified

取得或設定布林值,指定是否已修改與這個 ObjectSecurity 物件關聯的存取規則。

(繼承來源 ObjectSecurity)
AccessRuleType

取得 FileSystemSecurity 類別用來表示存取規則的列舉型別。

(繼承來源 FileSystemSecurity)
AreAccessRulesCanonical

取得布林值,指定與這個 ObjectSecurity 物件相關聯的存取規則是否為標準順序。

(繼承來源 ObjectSecurity)
AreAccessRulesProtected

取得布林值,指定是否保護與這個 ObjectSecurity 物件相關聯的判別存取控制清單 (DACL)。

(繼承來源 ObjectSecurity)
AreAuditRulesCanonical

取得布林值,指定與 ObjectSecurity 物件相關聯的稽核規則是否為標準順序。

(繼承來源 ObjectSecurity)
AreAuditRulesProtected

取得布林值,指定是否保護與這個 ObjectSecurity 物件相關聯的系統存取控制清單 (SACL)。

(繼承來源 ObjectSecurity)
AuditRulesModified

取得或設定布林值,指定是否已修改與這個 ObjectSecurity 物件關聯的稽核規則。

(繼承來源 ObjectSecurity)
AuditRuleType

取得 FileSystemSecurity 類別用來表示稽核規則的類型。

(繼承來源 FileSystemSecurity)
GroupModified

取得或設定布林值,指定是否已修改與安全物件關聯的群組。

(繼承來源 ObjectSecurity)
IsContainer

取得可指定這個 ObjectSecurity 物件是否為容器物件的布林值。

(繼承來源 ObjectSecurity)
IsDS

取得可指定這個 ObjectSecurity 物件是否為目錄物件的布林值。

(繼承來源 ObjectSecurity)
OwnerModified

取得或設定布林值,指定是否已修改安全物件的擁有者。

(繼承來源 ObjectSecurity)
SecurityDescriptor

取得此執行個體的安全性描述元。

(繼承來源 ObjectSecurity)

方法

AccessRuleFactory(IdentityReference, Int32, Boolean, InheritanceFlags, PropagationFlags, AccessControlType)

以指定的存取權限、存取控制和旗標,初始化 FileSystemAccessRule 類別 (用於表示指定使用者的新存取控制規則) 的新執行個體。

(繼承來源 FileSystemSecurity)
AddAccessRule(AccessRule)

將指定的存取規則加入與這個 CommonObjectSecurity 物件相關聯的 Discretionary 存取控制清單 (DACL)。

(繼承來源 CommonObjectSecurity)
AddAccessRule(FileSystemAccessRule)

將指定的存取控制清單 (ACL) 使用權限加入至目前的檔案或目錄。

(繼承來源 FileSystemSecurity)
AddAuditRule(AuditRule)

將指定的稽核規則加入與這個 CommonObjectSecurity 物件相關聯的系統存取控制清單 (SACL)。

(繼承來源 CommonObjectSecurity)
AddAuditRule(FileSystemAuditRule)

將指定的稽核規則加入至目前的檔案或目錄。

(繼承來源 FileSystemSecurity)
AuditRuleFactory(IdentityReference, Int32, Boolean, InheritanceFlags, PropagationFlags, AuditFlags)

初始化 FileSystemAuditRule 類別 (表示指定之使用者的指定稽核規則) 的新執行個體。

(繼承來源 FileSystemSecurity)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetAccessRules(Boolean, Boolean, Type)

取得與指定之安全識別項相關聯的存取規則集合。

(繼承來源 CommonObjectSecurity)
GetAuditRules(Boolean, Boolean, Type)

取得與指定之安全識別項相關聯的稽核規則集合。

(繼承來源 CommonObjectSecurity)
GetGroup(Type)

取得與指定擁有者相關聯的主要群組。

(繼承來源 ObjectSecurity)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetOwner(Type)

取得與指定的主要群組關聯的擁有者。

(繼承來源 ObjectSecurity)
GetSecurityDescriptorBinaryForm()

傳回表示此 ObjectSecurity 物件之安全性描述元資訊的位元組值陣列。

(繼承來源 ObjectSecurity)
GetSecurityDescriptorSddlForm(AccessControlSections)

傳回與這個 ObjectSecurity 物件相關聯的安全性描述元指定區段的安全性描述元定義語言 (SDDL) 表示法。

(繼承來源 ObjectSecurity)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ModifyAccess(AccessControlModification, AccessRule, Boolean)

將指定的修改套用至與這個 CommonObjectSecurity 物件關聯的判別存取控制清單 (DACL)。

(繼承來源 CommonObjectSecurity)
ModifyAccessRule(AccessControlModification, AccessRule, Boolean)

將指定的修改套用至與這個 ObjectSecurity 物件關聯的判別存取控制清單 (DACL)。

(繼承來源 ObjectSecurity)
ModifyAudit(AccessControlModification, AuditRule, Boolean)

將指定的修改套用至與這個 CommonObjectSecurity 物件關聯的系統存取控制清單 (SACL)。

(繼承來源 CommonObjectSecurity)
ModifyAuditRule(AccessControlModification, AuditRule, Boolean)

將指定的修改套用至與這個 ObjectSecurity 物件關聯的系統存取控制清單 (SACL)。

(繼承來源 ObjectSecurity)
Persist(Boolean, String, AccessControlSections)

將與這個 ObjectSecurity 物件相關聯之安全性描述元的指定區段儲存到永久儲存區。 建議傳遞至建構函式和保存方法之 includeSections 參數的值應完全相同。

(繼承來源 ObjectSecurity)
Persist(SafeHandle, AccessControlSections)

將與這個 NativeObjectSecurity 物件相關聯之安全性描述元的指定區段儲存到永久儲存區。 建議您保留傳遞到建構函式的 includeSections 參數值,並維持方法相同。

(繼承來源 NativeObjectSecurity)
Persist(SafeHandle, AccessControlSections, Object)

將與這個 NativeObjectSecurity 物件相關聯之安全性描述元的指定區段儲存到永久儲存區。 建議傳遞至建構函式和保存方法之 includeSections 參數的值應完全相同。

(繼承來源 NativeObjectSecurity)
Persist(String, AccessControlSections)

將與這個 NativeObjectSecurity 物件相關聯之安全性描述元的指定區段儲存到永久儲存區。 建議傳遞至建構函式和保存方法之 includeSections 參數的值應完全相同。

(繼承來源 NativeObjectSecurity)
Persist(String, AccessControlSections, Object)

將與這個 NativeObjectSecurity 物件相關聯之安全性描述元的指定區段儲存到永久儲存區。 建議傳遞至建構函式和保存方法之 includeSections 參數的值應完全相同。

(繼承來源 NativeObjectSecurity)
PurgeAccessRules(IdentityReference)

移除與指定之 IdentityReference 相關聯的所有存取規則。

(繼承來源 ObjectSecurity)
PurgeAuditRules(IdentityReference)

移除與指定之 IdentityReference 相關聯的所有稽核規則。

(繼承來源 ObjectSecurity)
ReadLock()

鎖定這個 ObjectSecurity 物件的讀取權限。

(繼承來源 ObjectSecurity)
ReadUnlock()

解除鎖定這個 ObjectSecurity 物件的讀取權限。

(繼承來源 ObjectSecurity)
RemoveAccessRule(AccessRule)

從與這個 CommonObjectSecurity 物件相關聯的 Discretionary 存取控制清單 (DACL),移除包含與指定存取規則相同安全識別項和存取遮罩的存取規則。

(繼承來源 CommonObjectSecurity)
RemoveAccessRule(FileSystemAccessRule)

從目前的檔案或目錄中,移除所有相符的允許或拒絕存取控制清單 (ACL) 使用權限。

(繼承來源 FileSystemSecurity)
RemoveAccessRuleAll(AccessRule)

從與這個 CommonObjectSecurity 物件相關聯的 Discretionary 存取控制清單 (DACL),移除包含與指定存取規則相同安全識別項的所有存取規則。

(繼承來源 CommonObjectSecurity)
RemoveAccessRuleAll(FileSystemAccessRule)

從目前的檔案或目錄中,移除指定之使用者的所有存取控制清單 (ACL) 使用權限。

(繼承來源 FileSystemSecurity)
RemoveAccessRuleSpecific(AccessRule)

從與這個 CommonObjectSecurity 物件相關聯的 Discretionary 存取控制清單 (DACL),移除與指定存取規則完全相符的所有存取規則。

(繼承來源 CommonObjectSecurity)
RemoveAccessRuleSpecific(FileSystemAccessRule)

從目前的檔案或目錄中,移除相符的單一允許或拒絕存取控制清單 (ACL) 使用權限。

(繼承來源 FileSystemSecurity)
RemoveAuditRule(AuditRule)

從與這個 CommonObjectSecurity 物件相關聯的系統存取控制清單 (SACL),移除包含與指定稽核規則相同安全識別項和存取遮罩的稽核規則。

(繼承來源 CommonObjectSecurity)
RemoveAuditRule(FileSystemAuditRule)

從目前的檔案或目錄中,移除所有相符的允許或拒絕稽核規則。

(繼承來源 FileSystemSecurity)
RemoveAuditRuleAll(AuditRule)

從與這個 CommonObjectSecurity 物件相關聯的系統存取控制清單 (SACL),移除包含與指定稽核規則相同安全識別項的所有稽核規則。

(繼承來源 CommonObjectSecurity)
RemoveAuditRuleAll(FileSystemAuditRule)

從目前的檔案或目錄中,移除指定之使用者的所有稽核規則。

(繼承來源 FileSystemSecurity)
RemoveAuditRuleSpecific(AuditRule)

從與這個 CommonObjectSecurity 物件相關聯的系統存取控制清單 (SACL),移除與指定稽核規則完全相符的所有稽核規則。

(繼承來源 CommonObjectSecurity)
RemoveAuditRuleSpecific(FileSystemAuditRule)

從目前的檔案或目錄中,移除相符的單一允許或拒絕稽核規則。

(繼承來源 FileSystemSecurity)
ResetAccessRule(AccessRule)

移除與這個 CommonObjectSecurity 物件相關聯之 Discretionary 存取控制清單 (DACL) 中的所有存取規則,然後加入指定的存取規則。

(繼承來源 CommonObjectSecurity)
ResetAccessRule(FileSystemAccessRule)

將指定的存取控制清單 (ACL) 使用權限加入至目前的檔案或目錄,並移除所有相符的 ACL 使用權限。

(繼承來源 FileSystemSecurity)
SetAccessRule(AccessRule)

從與這個 CommonObjectSecurity 物件相關聯之 Discretionary 存取控制清單 (DACL),移除包含與指定存取規則相同安全識別項和限定詞的所有存取規則,然後加入指定的存取規則。

(繼承來源 CommonObjectSecurity)
SetAccessRule(FileSystemAccessRule)

設定目前檔案或目錄之指定的存取控制清單 (ACL) 使用權限。

(繼承來源 FileSystemSecurity)
SetAccessRuleProtection(Boolean, Boolean)

設定或移除與 ObjectSecurity 物件相關聯的存取規則保護。 受保護的存取規則無法透過繼承來由父物件所修改。

(繼承來源 ObjectSecurity)
SetAuditRule(AuditRule)

從與這個 CommonObjectSecurity 物件相關聯之系統存取控制清單 (SACL),移除包含與指定稽核規則相同安全識別項和限定詞的所有稽核規則,然後加入指定的稽核規則。

(繼承來源 CommonObjectSecurity)
SetAuditRule(FileSystemAuditRule)

設定目前檔案或目錄的指定稽核規則。

(繼承來源 FileSystemSecurity)
SetAuditRuleProtection(Boolean, Boolean)

設定或移除與 ObjectSecurity 物件相關聯的稽核規則保護。 受保護的稽核規則無法透過繼承來由父物件所修改。

(繼承來源 ObjectSecurity)
SetGroup(IdentityReference)

設定與 ObjectSecurity 物件相關聯的安全性描述元主要群組。

(繼承來源 ObjectSecurity)
SetOwner(IdentityReference)

設定與 ObjectSecurity 物件相關聯的安全性描述元擁有者。

(繼承來源 ObjectSecurity)
SetSecurityDescriptorBinaryForm(Byte[])

從指定的位元組值陣列,設定這個 ObjectSecurity 物件的安全性描述元。

(繼承來源 ObjectSecurity)
SetSecurityDescriptorBinaryForm(Byte[], AccessControlSections)

從指定的位元組值陣列,設定這個 ObjectSecurity 物件之安全性描述元的指定區段。

(繼承來源 ObjectSecurity)
SetSecurityDescriptorSddlForm(String)

從指定的安全性描述元定義語言 (SDDL) 字串,設定這個 ObjectSecurity 物件的安全性描述元。

(繼承來源 ObjectSecurity)
SetSecurityDescriptorSddlForm(String, AccessControlSections)

從指定的安全性描述元定義語言 (SDDL) 字串,設定這個 ObjectSecurity 物件的安全性描述元之指定區段。

(繼承來源 ObjectSecurity)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
WriteLock()

鎖定這個 ObjectSecurity 物件的寫入權限。

(繼承來源 ObjectSecurity)
WriteUnlock()

解除鎖定這個 ObjectSecurity 物件的寫入權限。

(繼承來源 ObjectSecurity)

適用於