FileInfo.GetAccessControl 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 FileSecurity 對象,這個物件會封裝目前 FileInfo 物件所描述之檔案的訪問控制清單 (ACL) 專案。
多載
GetAccessControl() |
取得 FileSecurity 對象,這個物件會封裝目前 FileInfo 物件所描述之檔案的訪問控制清單 (ACL) 專案。 |
GetAccessControl(AccessControlSections) |
取得 FileSecurity 對象,這個物件會封裝目前 FileInfo 物件所描述之檔案的指定訪問控制清單 (ACL) 項目類型。 |
GetAccessControl()
取得 FileSecurity 對象,這個物件會封裝目前 FileInfo 物件所描述之檔案的訪問控制清單 (ACL) 專案。
public:
System::Security::AccessControl::FileSecurity ^ GetAccessControl();
public System.Security.AccessControl.FileSecurity GetAccessControl ();
member this.GetAccessControl : unit -> System.Security.AccessControl.FileSecurity
Public Function GetAccessControl () As FileSecurity
傳回
封裝目前檔案之訪問控制規則的 FileSecurity 物件。
例外狀況
開啟檔案時發生 I/O 錯誤。
目前的系統帳戶沒有系統管理許可權。
找不到檔案。
範例
下列程式代碼範例會使用 GetAccessControl 方法和 SetAccessControl 方法來新增和移除檔案中的訪問控制清單 (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(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(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.
'
備註
使用 GetAccessControl 方法來擷取目前檔案的訪問控制清單 (ACL) 專案。
ACL 描述具有或沒有指定檔案上特定動作許可權的個人和群組。 如需詳細資訊,請參閱 如何:新增或移除存取控制清單項目。
適用於
GetAccessControl(AccessControlSections)
取得 FileSecurity 對象,這個物件會封裝目前 FileInfo 物件所描述之檔案的指定訪問控制清單 (ACL) 項目類型。
public:
System::Security::AccessControl::FileSecurity ^ GetAccessControl(System::Security::AccessControl::AccessControlSections includeSections);
public System.Security.AccessControl.FileSecurity GetAccessControl (System.Security.AccessControl.AccessControlSections includeSections);
member this.GetAccessControl : System.Security.AccessControl.AccessControlSections -> System.Security.AccessControl.FileSecurity
Public Function GetAccessControl (includeSections As AccessControlSections) As FileSecurity
參數
- includeSections
- AccessControlSections
其中一個 AccessControlSections 值,指定要擷取的訪問控制專案群組。
傳回
封裝目前檔案之訪問控制規則的 FileSecurity 物件。
例外狀況
開啟檔案時發生 I/O 錯誤。
目前的系統帳戶沒有系統管理許可權。
找不到檔案。
備註
使用 GetAccessControl 方法來擷取目前檔案的訪問控制清單 (ACL) 專案。
ACL 描述具有或沒有指定檔案上特定動作許可權的個人和群組。 如需詳細資訊,請參閱 如何:新增或移除存取控制清單項目。