FileSystemSecurity.AddAccessRule(FileSystemAccessRule) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的访问控制列表 (ACL) 权限添加到当前文件或目录。
public:
void AddAccessRule(System::Security::AccessControl::FileSystemAccessRule ^ rule);
public void AddAccessRule (System.Security.AccessControl.FileSystemAccessRule rule);
override this.AddAccessRule : System.Security.AccessControl.FileSystemAccessRule -> unit
Public Sub AddAccessRule (rule As FileSystemAccessRule)
参数
- rule
- FileSystemAccessRule
表示要添加到文件或目录的访问控制列表(ACL)权限的 FileSystemAccessRule 对象。
例外
rule
参数 null
。
示例
下面的代码示例使用 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
注解
AddAccessRule 方法将新规则添加到 FileSystemSecurity 对象中包含的规则列表中。
如果指定规则已存在访问控制列表(ACL),AddAccessRule 方法仍将添加规则,但有一个例外:使用 Deny 枚举值创建的 FileSystemAccessRule 对象取代使用 Allow 枚举值创建的对象。
使用以下依赖于 .NET 实现的方法从文件添加或检索 ACL 信息:
添加访问规则而不设置 Synchronize 标志时,Synchronize 标志将自动添加到规则中。 如果稍后在不指定 Synchronize 标志的情况下删除规则,将自动删除该标志。