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 类是访问控制项的抽象, (ACE) 组成 DACL 和 SCL。

FileSecurity 隐藏了 DACL 和 SCL 的许多详细信息;无需担心 ACE 排序或 NULL DACLS。

FileSecurity使用 类检索、添加或更改表示文件的 DACL 和 SACL 的访问规则。

若要对文件保留新的或更改的访问或审核规则,请使用 SetAccessControl 方法。 若要从现有文件检索访问或审核规则,请使用 GetAccessControl 方法。

构造函数

FileSecurity()

初始化 FileSecurity 类的新实例。

FileSecurity(String, AccessControlSections)

使用 FileSecurity 枚举的指定值从指定文件初始化 AccessControlSections 类的新实例。

属性

AccessRightType

获取 FileSystemSecurity 类用于表示访问规则的枚举。

(继承自 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 对象关联的自由访问控制列表 (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 对象关联的安全描述符的指定部分保存到永久存储中。 我们建议传递给构造函数和 persist 方法的 includeSections 参数的值应完全相同。

(继承自 ObjectSecurity)
Persist(SafeHandle, AccessControlSections)

将与此 NativeObjectSecurity 对象关联的安全描述符的指定部分保存到永久存储中。 我们建议保持传递给构造函数和 persist 方法的 includeSections 参数的值完全相同。

(继承自 NativeObjectSecurity)
Persist(SafeHandle, AccessControlSections, Object)

将与此 NativeObjectSecurity 对象关联的安全描述符的指定部分保存到永久存储中。 我们建议传递给构造函数和 persist 方法的 includeSections 参数的值应完全相同。

(继承自 NativeObjectSecurity)
Persist(String, AccessControlSections)

将与此 NativeObjectSecurity 对象关联的安全描述符的指定部分保存到永久存储中。 我们建议传递给构造函数和 persist 方法的 includeSections 参数的值应完全相同。

(继承自 NativeObjectSecurity)
Persist(String, AccessControlSections, Object)

将与此 NativeObjectSecurity 对象关联的安全描述符的指定部分保存到永久存储中。 我们建议传递给构造函数和 persist 方法的 includeSections 参数的值应完全相同。

(继承自 NativeObjectSecurity)
PurgeAccessRules(IdentityReference)

删除与指定 IdentityReference 关联的所有访问规则。

(继承自 ObjectSecurity)
PurgeAuditRules(IdentityReference)

删除与指定 IdentityReference 关联的所有审核规则。

(继承自 ObjectSecurity)
ReadLock()

为读取访问锁定此 ObjectSecurity 对象。

(继承自 ObjectSecurity)
ReadUnlock()

解锁此 ObjectSecurity 对象以进行读取访问。

(继承自 ObjectSecurity)
RemoveAccessRule(AccessRule)

从与此 CommonObjectSecurity 对象关联的自由访问控制列表 (DACL) 中移除与指定的访问规则具有相同安全性标识符和访问掩码的访问规则。

(继承自 CommonObjectSecurity)
RemoveAccessRule(FileSystemAccessRule)

从当前文件或目录移除所有匹配的允许或拒绝访问控制列表 (ACL) 权限。

(继承自 FileSystemSecurity)
RemoveAccessRuleAll(AccessRule)

从与此 CommonObjectSecurity 对象关联的自由访问控制列表 (DACL) 中移除与指定的访问规则具有相同安全性标识符的所有访问规则。

(继承自 CommonObjectSecurity)
RemoveAccessRuleAll(FileSystemAccessRule)

从当前文件或目录移除指定用户的所有访问控制列表 (ACL) 权限。

(继承自 FileSystemSecurity)
RemoveAccessRuleSpecific(AccessRule)

从与此 CommonObjectSecurity 对象关联的自由访问控制列表 (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 对象关联的自由访问控制列表 (DACL) 中移除所有访问规则,然后添加指定的访问规则。

(继承自 CommonObjectSecurity)
ResetAccessRule(FileSystemAccessRule)

将指定的访问控制列表 (ACL) 权限添加到当前文件或目录,并移除所有匹配的 ACL 权限。

(继承自 FileSystemSecurity)
SetAccessRule(AccessRule)

从与此 CommonObjectSecurity 对象关联的自由访问控制列表 (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)

适用于