File.SetAccessControl(String, FileSecurity) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
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);
}
}
}
open System.IO
open System.Security.AccessControl
// Adds an ACL entry on the specified file for the specified account.
let addFileSecurity fileName (account: string) rights controlType =
// Get a FileSecurity object that represents the
// current security settings.
let fSecurity = File.GetAccessControl fileName
// Add the FileSystemAccessRule to the security settings.
FileSystemAccessRule(account, rights, controlType)
|> fSecurity.AddAccessRule
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity)
// Removes an ACL entry on the specified file for the specified account.
let removeFileSecurity fileName (account: string) rights controlType =
// Get a FileSecurity object that represents the
// current security settings.
let fSecurity = File.GetAccessControl fileName
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(FileSystemAccessRule(account, rights, controlType))
|> ignore
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity)
let fileName = "test.xml"
printfn $"Adding access control entry for {fileName}"
// Add the access control entry to the file.
addFileSecurity fileName @"DomainName\AccountName" FileSystemRights.ReadData AccessControlType.Allow
printfn $"Removing access control entry from {fileName}"
// Remove the access control entry from the file.
removeFileSecurity fileName @"DomainName\AccountName" FileSystemRights.ReadData AccessControlType.Allow
printfn "Done."
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 정보를 복사하려면 다음을 수행합니다.
메서드를 GetAccessControl 사용하여 소스 파일에서 개체를 검색 FileSecurity 합니다.
대상 파일에 대한 새 FileSecurity 개체를 Create.
원본 FileSecurity 개체의 GetSecurityDescriptorBinaryForm 또는 GetSecurityDescriptorSddlForm 메서드를 사용하여 ACL 정보를 검색합니다.
또는 SetSecurityDescriptorSddlForm 메서드를 SetSecurityDescriptorBinaryForm 사용하여 3단계에서 검색한 정보를 대상 FileSecurity 개체에 복사합니다.
메서드를 사용하여 대상 FileSecurity 개체를 대상 파일로 SetAccessControl 설정합니다.
NTFS 환경에서 ReadAttributes 및 ReadExtendedAttributes 는 사용자에게 부모 폴더에 대한 권한이 있는 ListDirectory 경우 사용자에게 부여됩니다. 및 ReadExtendedAttributes를 거부 ReadAttributes 하려면 부모 디렉터리에 대해 를 거부 ListDirectory 합니다.
적용 대상
추가 정보
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기