다음을 통해 공유


FileSystemSecurity.RemoveAccessRule(FileSystemAccessRule) 메서드

정의

현재 파일 또는 디렉터리에서 일치하는 모든 ACL(액세스 제어 목록) 권한을 제거합니다.

public:
 bool RemoveAccessRule(System::Security::AccessControl::FileSystemAccessRule ^ rule);
public bool RemoveAccessRule (System.Security.AccessControl.FileSystemAccessRule rule);
override this.RemoveAccessRule : System.Security.AccessControl.FileSystemAccessRule -> bool
Public Function RemoveAccessRule (rule As FileSystemAccessRule) As Boolean

매개 변수

rule
FileSystemAccessRule

파일 또는 디렉터리에서 제거할 ACL(액세스 제어 목록) 권한을 나타내는 FileSystemAccessRule 개체입니다.

반환

액세스 규칙이 제거되었는지 true. 그렇지 않으면 false.

예외

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

설명

RemoveAccessRule 메서드는 현재 FileSystemSecurity 개체에서 일치하는 모든 Deny 액세스 규칙 또는 일치하는 모든 Allow 액세스 규칙을 제거합니다. 예를 들어 이 메서드를 사용하여 Deny 값, Read 값 및 사용자 계정을 사용하여 만든 FileSystemAccessRule 개체를 전달하여 사용자에 대한 모든 Deny 액세스 규칙을 제거할 수 있습니다. 이렇게 하면 RemoveAccessRule 메서드는 Read 값 또는 Write 값을 지정하는 거부 규칙을 제거합니다.

다음 .NET 구현 종속 메서드를 사용하여 파일에서 ACL 정보를 추가하거나 검색합니다.

.NET 구현 규칙 추가 규칙 검색
.그물 FileSystemAclExtensions.SetAccessControl(FileInfo, FileSecurity) FileSystemAclExtensions.GetAccessControl(FileInfo)
.NET Framework FileInfo.SetAccessControl(FileSecurity) FileInfo.GetAccessControl()

Synchronize 플래그를 설정하지 않고 액세스 규칙을 추가하면 Synchronize 플래그가 규칙에 자동으로 추가됩니다. Synchronize 플래그를 지정하지 않고 나중에 규칙을 제거하면 플래그가 자동으로 제거됩니다.

적용 대상