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 類別的新實例。 |