FileSystemSecurity 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示文件或目录的访问控制和审核安全性。
public ref class FileSystemSecurity abstract : System::Security::AccessControl::NativeObjectSecurity
public abstract class FileSystemSecurity : System.Security.AccessControl.NativeObjectSecurity
[System.Security.SecurityCritical]
public abstract class FileSystemSecurity : System.Security.AccessControl.NativeObjectSecurity
type FileSystemSecurity = class
inherit NativeObjectSecurity
[<System.Security.SecurityCritical>]
type FileSystemSecurity = class
inherit NativeObjectSecurity
Public MustInherit Class FileSystemSecurity
Inherits NativeObjectSecurity
- 继承
- 派生
- 属性
示例
下面的代码示例使用 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
注解
该FileSystemSecurity类是和FileSecurity类的基类DirectorySecurity。 这些类表示系统文件或目录的所有访问权限,并定义如何审核访问尝试。
该 FileSystemSecurity 类将访问和审核权限表示为一组规则。 每个访问规则都由一个 FileSystemAccessRule 对象表示,而每个审核规则由一个 FileSystemAuditRule 对象表示。
FileSystemSecurity 类是基础Microsoft Windows文件安全系统的抽象。 在此系统中,每个文件或目录都有一个自由访问控制列表(DACL),用于控制对文件或目录的访问,以及一个系统访问控制列表(SACL),该列表指定审核的访问控制尝试。 类FileSystemAccessRuleFileSystemAuditRule是构成 DACL 和 SCL 的访问控制条目(ACE)的抽象。
该 FileSystemSecurity 类隐藏 DACL 和 SCL 的许多详细信息;无需担心 ACE 排序或 NULL DACLS。
使用以下.NET依赖实现的方法从文件中添加或检索 ACL 信息: