FileInfo.SetAccessControl(FileSecurity) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
FileSecurity オブジェクトが示すアクセス制御リスト (ACL) エントリを、現在の FileInfo オブジェクトが示すファイルに適用します。
public:
void SetAccessControl(System::Security::AccessControl::FileSecurity ^ fileSecurity);
public void SetAccessControl (System.Security.AccessControl.FileSecurity fileSecurity);
member this.SetAccessControl : System.Security.AccessControl.FileSecurity -> unit
Public Sub SetAccessControl (fileSecurity As FileSecurity)
パラメーター
- fileSecurity
- FileSecurity
現在のファイルに適用するアクセス制御リスト (ACL) エントリを示す FileSecurity オブジェクト。
例外
fileSecurity
パラメーターが null
です。
ファイルを検出または変更できませんでした。
現在のプロセスには、ファイルを開くために必要なアクセス権がありません。
例
次のコード例では、 メソッドと メソッドをGetAccessControlSetAccessControl使用して ACL エントリを追加し、ファイルから削除します。 この例を実行するには、有効なユーザーまたはグループ アカウントを指定する必要があります。
#using <System.Security.dll>
using namespace System;
using namespace System::IO;
using namespace System::Security::AccessControl;
using namespace System::Security::Principal;
// Adds an ACL entry on the specified file for the specified account.
static void AddFileSecurity(String^ fileName, String^ account,
FileSystemRights^ rights,
AccessControlType^ controlType)
{
// Create a new FileInfo object.
FileInfo^ fInfo = gcnew FileInfo(fileName);
if (!fInfo->Exists)
{
fInfo->Create();
}
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity^ fSecurity = fInfo->GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity->AddAccessRule(gcnew FileSystemAccessRule(account,
*rights, *controlType));
// Set the new access settings.
fInfo->SetAccessControl(fSecurity);
}
// Removes an ACL entry on the specified file for the specified account.
static void RemoveFileSecurity(String^ fileName, String^ account,
FileSystemRights^ rights,
AccessControlType^ controlType)
{
// Create a new FileInfo object.
FileInfo^ fInfo = gcnew FileInfo(fileName);
if (!fInfo->Exists)
{
fInfo->Create();
}
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity^ fSecurity = fInfo->GetAccessControl();
// Remove the FileSystemAccessRule from the security settings.
fSecurity->RemoveAccessRule(gcnew FileSystemAccessRule(account,
*rights, *controlType));
// Set the new access settings.
fInfo->SetAccessControl(fSecurity);
}
int main()
{
try
{
String^ fileName = "c:\\test.xml";
Console::WriteLine("Adding access control entry for " +
fileName);
// Add the access control entry to the file.
// Before compiling this snippet, change MyDomain to your
// domain name and MyAccessAccount to the name
// you use to access your domain.
AddFileSecurity(fileName, "MyDomain\\MyAccessAccount",
FileSystemRights::ReadData, AccessControlType::Allow);
Console::WriteLine("Removing access control entry from " +
fileName);
// Remove the access control entry from the file.
// Before compiling this snippet, change MyDomain to your
// domain name and MyAccessAccount to the name
// you use to access your domain.
RemoveFileSecurity(fileName, "MyDomain\\MyAccessAccount",
FileSystemRights::ReadData, AccessControlType::Allow);
Console::WriteLine("Done.");
}
catch (Exception^ e)
{
Console::WriteLine(e);
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Adding access control entry for c:\test.xml
//Removing access control entry from c:\test.xml
//Done.
//
using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string FileName = "c:/test.xml";
Console.WriteLine("Adding access control entry for " + FileName);
// Add the access control entry to the file.
// Before compiling this snippet, change MyDomain to your
// domain name and MyAccessAccount to the name
// you use to access your domain.
AddFileSecurity(FileName, @"MyDomain\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow);
Console.WriteLine("Removing access control entry from " + FileName);
// Remove the access control entry from the file.
// Before compiling this snippet, change MyDomain to your
// domain name and MyAccessAccount to the name
// you use to access your domain.
RemoveFileSecurity(FileName, @"MyDomain\MyAccessAccount", 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)
{
// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
fInfo.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)
{
// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
fInfo.SetAccessControl(fSecurity);
}
}
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Adding access control entry for c:\test.xml
//Removing access control entry from c:\test.xml
//Done.
//
Imports System.IO
Imports System.Security.AccessControl
Module FileExample
Sub Main()
Try
Dim FileName As String = "c:\test.xml"
Console.WriteLine("Adding access control entry for " & FileName)
' Add the access control entry to the file.
' Before compiling this snippet, change MyDomain to your
' domain name and MyAccessAccount to the name
' you use to access your domain.
AddFileSecurity(FileName, "MyDomain\\MyAccessAccount", FileSystemRights.ReadData, AccessControlType.Allow)
Console.WriteLine("Removing access control entry from " & FileName)
' Remove the access control entry from the file.
' Before compiling this snippet, change MyDomain to your
' domain name and MyAccessAccount to the name
' you use to access your domain.
RemoveFileSecurity(FileName, "MyDomain\\MyAccessAccount", 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)
' Create a new FileInfo object.
Dim fInfo As New FileInfo(FileName)
' Get a FileSecurity object that represents the
' current security settings.
Dim fSecurity As FileSecurity = fInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))
' Set the new access settings.
fInfo.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)
' Create a new FileInfo object.
Dim fInfo As New FileInfo(FileName)
' Get a FileSecurity object that represents the
' current security settings.
Dim fSecurity As FileSecurity = fInfo.GetAccessControl()
' Add the FileSystemAccessRule to the security settings.
fSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))
' Set the new access settings.
fInfo.SetAccessControl(fSecurity)
End Sub
End Module
'This code produces output similar to the following;
'results may vary based on the computer/file structure/etc.:
'
'Adding access control entry for c:\test.xml
'Removing access control entry from c:\test.xml
'Done.
'
注釈
メソッドは SetAccessControl 、非暗号化 ACL リストを表す現在のファイルにアクセス制御リスト (ACL) エントリを適用します。
ACL エントリをファイルに SetAccessControl 追加または削除する必要がある場合は常に、 メソッドを使用します。
注意事項
パラメーターに指定された ACL は、 fileSecurity
ファイルの既存の ACL を置き換えます。 新しいユーザーのアクセス許可を追加するには、 メソッドを GetAccessControl 使用して既存の ACL を取得し、それを変更してから、 を使用 SetAccessControl してファイルに適用し直します。
ACL は、特定のファイルに対する特定のアクションに対する権限を持っている、または持たない個人やグループを記述します。 詳細については、「方法: アクセス制御リスト エントリを追加または削除する」を参照してください。
メソッドは SetAccessControl 、オブジェクトの作成後に変更されたオブジェクトのみを FileSecurity 保持します。 オブジェクトが FileSecurity 変更されていない場合、そのオブジェクトはファイルに保存されません。 したがって、あるファイルからオブジェクトを FileSecurity 取得し、同じオブジェクトを別のファイルに再適用することはできません。
あるファイルから別のファイルに ACL 情報をコピーするには:
メソッドを GetAccessControl 使用して、 FileSecurity ソース ファイルからオブジェクトを取得します。
コピー先ファイルの新FileSecurityしいオブジェクトをCreateします。
ACL 情報をGetSecurityDescriptorBinaryForm取得するには、ソース FileSecurity オブジェクトの メソッドまたは GetSecurityDescriptorSddlForm メソッドを使用します。
または SetSecurityDescriptorSddlForm メソッドをSetSecurityDescriptorBinaryForm使用して、手順 3 で取得した情報をコピー先FileSecurityオブジェクトにコピーします。
メソッドを使用して、コピー先 FileSecurity オブジェクトをコピー先ファイルに SetAccessControl 設定します。
適用対象
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示