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 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是 和 DirectorySecurity 类的FileSecurity基类。 这些类表示系统文件或目录的所有访问权限,并定义如何审核访问尝试。
类 FileSystemSecurity 将访问和审核权限表示为一组规则。 每个访问规则由一个 FileSystemAccessRule 对象表示,而每个审核规则由一个 FileSystemAuditRule 对象表示。
类FileSystemSecurity是基础Microsoft Windows 文件安全系统的抽象。 在此系统中,每个文件或目录都有一个可自由支配的访问控制列表 (DACL) ,该列表控制对文件或目录的访问,以及一个系统访问控制列表 (SACL) ,该列表指定要审核的访问控制尝试。 FileSystemAccessRule和 FileSystemAuditRule 类是访问控制条目的抽象, (ACE) 组成 DACL 和 SCL。
类 FileSystemSecurity 隐藏了 DACL 和 SCL 的许多详细信息;你不必担心 ACE 排序或 NULL DACLS。
若要将新的或更改的访问控制列表 (ACL) 信息保存到文件中,请使用 SetAccessControl 或 SetAccessControl 方法。 若要将新的或更改的 ACL 信息保存到目录,请使用 SetAccessControl 或 SetAccessControl 方法。
若要从文件检索 ACL 信息,请使用 GetAccessControl 或 GetAccessControl 方法。 若要从目录中检索 ACL 信息,请使用 GetAccessControl 或 GetAccessControl 方法。
属性
方法
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈