FileInfo.SetAccessControl(FileSecurity) Yöntem

Tanım

Bir nesne tarafından FileSecurity açıklanan erişim denetimi listesi (ACL) girdilerini geçerli FileInfo nesne tarafından açıklanan dosyaya uygular.

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)

Parametreler

fileSecurity
FileSecurity

FileSecurity Geçerli dosyaya uygulanacak erişim denetimi listesi (ACL) girdisini açıklayan nesne.

Özel durumlar

fileSecurity parametresi null'dir.

Dosya bulunamadı veya değiştirilemedi.

Geçerli işlemin dosyayı açmaya erişimi yok.

Örnekler

Aşağıdaki kod örneği, bir dosyaya GetAccessControl ACL girdisi ekleyip kaldırmak için yöntemini ve SetAccessControl yöntemini kullanır. Bu örneği çalıştırmak için geçerli bir kullanıcı veya grup hesabı sağlamanız gerekir.

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.
'

Açıklamalar

yöntemi, SetAccessControl geçerli dosyaya erişim denetimi listesi (ACL) girdilerini uygular ve bu girdiler, hiçbirine bağlı olmayan ACL listesini temsil eder.

Bir dosyaya SetAccessControl ACL girdileri eklemeniz veya dosyadan kaldırmanız gerektiğinde yöntemini kullanın.

Caution

parametresi için fileSecurity belirtilen ACL, dosyanın mevcut ACL'sinin yerini alır. Yeni bir kullanıcının izinlerini eklemek için yöntemini kullanarak GetAccessControl mevcut ACL'yi alın, değiştirin ve ardından dosyaya geri uygulamak için kullanın SetAccessControl .

ACL, belirli bir dosya üzerinde belirli eylemlere sahip olan veya olmayan kişileri ve grupları açıklar. Daha fazla bilgi için bkz . Nasıl yapılır: Erişim Denetimi Listesi Girdileri Ekleme veya Kaldırma.

SetAccessControl yöntemi yalnızca FileSecurity nesne oluşturulduktan sonra değiştirilmiş nesneleri kalıcı hale döndürür. Bir FileSecurity nesne değiştirilmediyse, bir dosyada kalıcı olmaz. Bu nedenle, bir dosyadan nesne almak ve aynı nesneyi başka bir FileSecurity dosyaya yeniden uygulamak mümkün değildir.

ACL bilgilerini bir dosyadan diğerine kopyalamak için:

  1. GetAccessControl Nesnesini kaynak dosyadan almak FileSecurity için yöntemini kullanın.

  2. Hedef dosya için yeni FileSecurity bir nesne oluşturun.

  3. GetSecurityDescriptorBinaryForm ACL bilgilerini almak için kaynak GetSecurityDescriptorSddlForm nesnenin veya FileSecurity yöntemini kullanın.

  4. SetSecurityDescriptorBinaryForm 3. adımda alınan bilgileri hedef SetSecurityDescriptorSddlForm nesneye kopyalamak için or FileSecurity yöntemini kullanın.

  5. yöntemini kullanarak hedef FileSecurity nesneyi hedef dosyaya SetAccessControl ayarlayın.

Şunlara uygulanır