FileInfo.SetAccessControl(FileSecurity) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Applique les entrées ACL décrites par un objet FileSecurity au fichier décrit par l'objet FileInfo actuel.
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)
Paramètres
- fileSecurity
- FileSecurity
Objet FileSecurity qui décrit une entrée de liste de contrôle d'accès (ACL) à appliquer au fichier actuel.
Exceptions
Le paramètre fileSecurity
a la valeur null
.
Le fichier est introuvable ou n’a pas pu être modifié.
Le processus en cours n’a pas l’accès requis pour ouvrir le fichier.
Exemples
L’exemple de code suivant utilise la GetAccessControl méthode et la SetAccessControl méthode pour ajouter, puis supprimer une entrée de liste de contrôle d’accès à un fichier. Vous devez entrer un compte d'utilisateur ou de groupe valide pour exécuter cet exemple.
#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.
'
Remarques
La SetAccessControl méthode applique des entrées de liste de contrôle d’accès (ACL) au fichier actuel qui représente la liste ACL non utilisée.
Utilisez la SetAccessControl méthode chaque fois que vous devez ajouter ou supprimer des entrées ACL d’un fichier.
Attention
La liste de contrôle d’accès spécifiée pour le fileSecurity
paramètre remplace la liste de contrôle d’accès existante pour le fichier. Pour ajouter des autorisations pour un nouvel utilisateur, utilisez la GetAccessControl méthode pour obtenir la liste de contrôle d’accès existante, la modifier, puis l’appliquer SetAccessControl à nouveau au fichier.
Une liste de contrôle d’accès décrit des individus et/ou des groupes qui ont, ou n’ont pas, des droits sur des actions spécifiques sur le fichier donné. Pour plus d'informations, consultez Comment : ajouter ou supprimer des entrées dans la liste de contrôle d'accès.
La SetAccessControl méthode conserve uniquement FileSecurity les objets qui ont été modifiés après la création de l’objet. Si un FileSecurity objet n’a pas été modifié, il n’est pas conservé dans un fichier. Par conséquent, il n’est pas possible de récupérer un objet à partir d’un FileSecurity fichier et de réappliquer le même objet dans un autre fichier.
Pour copier les informations de liste de contrôle d’accès d’un fichier vers un autre :
Utilisez la GetAccessControl méthode pour récupérer l’objet FileSecurity à partir du fichier source.
Créez un FileSecurity objet pour le fichier de destination.
Utilisez la GetSecurityDescriptorBinaryForm méthode ou GetSecurityDescriptorSddlForm de l’objet source FileSecurity pour récupérer les informations de liste de contrôle d’accès.
Utilisez la SetSecurityDescriptorBinaryForm méthode ou SetSecurityDescriptorSddlForm pour copier les informations récupérées à l’étape 3 vers l’objet de destination FileSecurity .
Définissez l’objet de destination FileSecurity sur le fichier de destination à l’aide de la SetAccessControl méthode .
S’applique à
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour