FileSecurity 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示文件的访问控制和审核安全性。 无法继承此类。
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 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)
{
FileInfo fileInfo = new(fileName);
FileSecurity fSecurity = fileInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
fileInfo.SetAccessControl(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)
{
FileInfo fileInfo = new(fileName);
FileSecurity fSecurity = fileInfo.GetAccessControl();
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
fileInfo.SetAccessControl(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)
Dim fileInfo As New FileInfo(fileName)
Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
Dim accessRule As New FileSystemAccessRule(account, rights, controlType)
fSecurity.AddAccessRule(accessRule)
' Set the new access settings.
fileInfo.SetAccessControl(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)
Dim fileInfo As New FileInfo(fileName)
Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()
' Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
rights, controlType))
' Set the new access settings.
fileInfo.SetAccessControl(fSecurity)
End Sub
End Module
注解
FileSecurity 类指定系统文件的访问权限以及如何审核访问尝试。 此类将访问和审核权限表示为一组规则。 每个访问规则由 FileSystemAccessRule 对象表示。 每个审核规则都由 FileSystemAuditRule 对象表示。
FileSecurity 类是基础Microsoft Windows 文件安全系统的抽象。 在此系统中,每个文件都有一个自由访问控制列表(DACL),用于控制对文件的访问,以及一个系统访问控制列表(SACL),该列表指定审核的访问控制尝试。 FileSystemAccessRule 和 FileSystemAuditRule 类是构成 DACL 和 SCL 的访问控制条目(ACE)的抽象。
FileSecurity 类隐藏 DACL 和 SCL 的许多详细信息;无需担心 ACE 排序或 NULL DACL。
使用 FileSecurity 类检索、添加或更改表示文件的 DACL 和 SACL 的访问规则。
使用以下依赖于 .NET 实现的方法从文件添加或检索访问或审核规则:
构造函数
FileSecurity() |
初始化 FileSecurity 类的新实例。 |
FileSecurity(String, AccessControlSections) |
使用 AccessControlSections 枚举的指定值从指定文件中初始化 FileSecurity 类的新实例。 |