File.SetAccessControl(String, FileSecurity) 메서드

정의

FileSecurity 개체에 설명된 ACL(액세스 제어 목록) 항목을 지정된 파일에 적용합니다.

public:
 static void SetAccessControl(System::String ^ path, System::Security::AccessControl::FileSecurity ^ fileSecurity);
public static void SetAccessControl (string path, System.Security.AccessControl.FileSecurity fileSecurity);
static member SetAccessControl : string * System.Security.AccessControl.FileSecurity -> unit
Public Shared Sub SetAccessControl (path As String, fileSecurity As FileSecurity)

매개 변수

path
String

ACL(액세스 제어 목록) 항목을 추가하거나 제거할 파일입니다.

fileSecurity
FileSecurity

FileSecurity 매개 변수에 설명된 파일에 적용할 ACL 항목을 설명하는 path 개체입니다.

예외

파일을 여는 동안 I/O 오류가 발생했습니다.

path 매개 변수가 null인 경우

파일을 찾을 수 없습니다.

path 매개 변수가 읽기 전용 파일을 지정합니다.

또는 현재 플랫폼이 해당 작업을 지원하지 않는 경우

또는 path 매개 변수가 디렉터리를 지정합니다.

또는 호출자에게 필요한 권한이 없는 경우

fileSecurity 매개 변수가 null인 경우

예제

다음 코드 예제에서는 및 SetAccessControl 메서드를 사용하여 GetAccessControl 파일에서 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

설명

이 메서드는 SetAccessControl ACL(액세스 제어 목록) 항목을 암호화되지 않은 ACL 목록을 나타내는 파일에 적용합니다.

주의

매개 변수에 지정된 ACL은 fileSecurity 파일에 대한 기존 ACL을 대체합니다. 새 사용자에 대한 권한을 추가하려면 메서드를 사용하여 GetAccessControl 기존 ACL을 가져오고 수정한 다음 파일에 다시 적용하는 데 사용합니다 SetAccessControl .

ACL은 지정된 파일에 대한 특정 작업에 대한 권한이 있거나 없는 개인 및/또는 그룹을 설명합니다. 자세한 내용은 방법: 액세스 제어 목록 항목 추가 또는 제거를 참조하세요.

이 메서드는 SetAccessControl 개체를 만든 후에 수정된 개체만 FileSecurity 유지합니다. 개체가 FileSecurity 수정되지 않은 경우 파일에 유지되지 않습니다. 따라서 한 파일에서 개체를 FileSecurity 검색하고 동일한 개체를 다른 파일에 다시 적용할 수 없습니다.

한 파일에서 다른 파일로 ACL 정보를 복사하려면 다음을 수행합니다.

  1. 메서드를 GetAccessControl 사용하여 원본 파일에서 개체를 검색 FileSecurity 합니다.

  2. 대상 파일에 대한 새 FileSecurity 개체를 만듭니다.

  3. 원본 FileSecurity 개체의 GetSecurityDescriptorBinaryForm 메서드 또는 GetSecurityDescriptorSddlForm 메서드를 사용하여 ACL 정보를 검색합니다.

  4. SetSecurityDescriptorBinaryForm 또는 SetSecurityDescriptorSddlForm 메서드를 사용하여 3단계에서 검색된 정보를 대상 FileSecurity 개체에 복사합니다.

  5. 메서드를 사용하여 대상 FileSecurity 개체를 대상 파일로 SetAccessControl 설정합니다.

NTFS 환경에서 ReadAttributes ReadExtendedAttributes 는 사용자에게 부모 폴더에 대한 권한이 있는 경우 사용자에게 ListDirectory 부여됩니다. 거부 ReadAttributes 하려면 ReadExtendedAttributes부모 디렉터리에 대해 거부 ListDirectory 합니다.

적용 대상

추가 정보