방법: 액세스 제어 목록 항목 추가 또는 제거
파일에서 ACL(액세스 제어 목록) 엔트리를 추가하거나 제거하려면 파일 또는 디렉터리에서 FileSecurity 또는 DirectorySecurity 개체를 가져와서 수정한 다음 파일이나 디렉터리에 다시 적용해야 합니다.
파일에서 ACL 엔트리를 추가하거나 제거하려면
GetAccessControl 메서드를 호출하여 파일의 현재 ACL 엔트리가 포함된 FileSecurity 개체를 가져옵니다.
1단계에서 반환된 FileSecurity 개체에서 ACL 엔트리를 추가하거나 제거합니다.
FileSecurity 개체를 SetAccessControl 메서드에 전달하여 변경 내용을 적용합니다.
디렉터리에서 ACL 엔트리를 추가하거나 제거하려면
GetAccessControl 메서드를 호출하여 디렉터리의 현재 ACL 엔트리가 포함된 DirectorySecurity 개체를 가져옵니다.
1단계에서 반환된 DirectorySecurity 개체에서 ACL 엔트리를 추가하거나 제거합니다.
DirectorySecurity 개체를 SetAccessControl 메서드에 전달하여 변경 내용을 적용합니다.
예제
Imports System
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
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);
}
}
}
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);
}
}
코드 컴파일
이 예제를 실행하려면 올바른 사용자 또는 그룹 계정을 제공해야 합니다. 이 예제에서는 File 개체를 사용하지만 FileInfo, Directory 및 DirectoryInfo 클래스에도 같은 절차가 사용됩니다.