Share via


FileSystemAccessRule Kelas

Definisi

Mewakili abstraksi entri kontrol akses (ACE) yang menentukan aturan akses untuk file atau direktori. Kelas ini tidak dapat diwariskan.

public ref class FileSystemAccessRule sealed : System::Security::AccessControl::AccessRule
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
[System.Security.SecurityCritical]
public sealed class FileSystemAccessRule : System.Security.AccessControl.AccessRule
type FileSystemAccessRule = class
    inherit AccessRule
[<System.Security.SecurityCritical>]
type FileSystemAccessRule = class
    inherit AccessRule
Public NotInheritable Class FileSystemAccessRule
Inherits AccessRule
Warisan
FileSystemAccessRule
Atribut

Contoh

Contoh kode berikut menggunakan FileSecurity kelas untuk menambahkan lalu menghapus entri kontrol akses (ACE) dari file. Anda harus menyediakan akun pengguna atau grup yang valid untuk menjalankan contoh ini.

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);
        }
    }
}
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

Keterangan

Kelas FileSystemAccessRule mewakili abstraksi entri kontrol akses (ACE) yang mendasar yang menentukan akun pengguna, jenis akses untuk menyediakan (baca, tulis, dan sebagainya), dan apakah akan mengizinkan atau menolak hak tersebut. Kelas ini juga dapat menentukan bagaimana aturan akses disebarluaskan ke objek anak.

FileSystemAccessRule Gunakan kelas untuk membuat aturan akses baru. Anda dapat mempertahankan aturan menggunakan FileSecurity kelas atau DirectorySecurity .

Konstruktor

FileSystemAccessRule(IdentityReference, FileSystemRights, AccessControlType)

Menginisialisasi instans FileSystemAccessRule baru kelas menggunakan referensi ke akun pengguna, nilai yang menentukan jenis operasi yang terkait dengan aturan akses, dan nilai yang menentukan apakah akan mengizinkan atau menolak operasi.

FileSystemAccessRule(IdentityReference, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

Menginisialisasi instans FileSystemAccessRule baru kelas menggunakan referensi ke akun pengguna, nilai yang menentukan jenis operasi yang terkait dengan aturan akses, nilai yang menentukan bagaimana hak diwariskan, nilai yang menentukan bagaimana hak disebarluaskan, dan nilai yang menentukan apakah akan mengizinkan atau menolak operasi.

FileSystemAccessRule(String, FileSystemRights, AccessControlType)

Menginisialisasi instans FileSystemAccessRule baru kelas menggunakan nama akun pengguna, nilai yang menentukan jenis operasi yang terkait dengan aturan akses, dan nilai yang menjelaskan apakah akan mengizinkan atau menolak operasi.

FileSystemAccessRule(String, FileSystemRights, InheritanceFlags, PropagationFlags, AccessControlType)

Menginisialisasi instans FileSystemAccessRule baru kelas menggunakan nama akun pengguna, nilai yang menentukan jenis operasi yang terkait dengan aturan akses, nilai yang menentukan bagaimana hak diwariskan, nilai yang menentukan bagaimana hak disebarluaskan, dan nilai yang menentukan apakah akan mengizinkan atau menolak operasi.

Properti

AccessControlType

Mendapatkan nilai yang AccessControlType terkait dengan objek ini AccessRule .

(Diperoleh dari AccessRule)
AccessMask

Mendapatkan masker akses untuk aturan ini.

(Diperoleh dari AuthorizationRule)
FileSystemRights

Mendapatkan bendera yang FileSystemRights terkait dengan objek saat ini FileSystemAccessRule .

IdentityReference

Mendapatkan aturan IdentityReference ini yang berlaku.

(Diperoleh dari AuthorizationRule)
InheritanceFlags

Mendapatkan nilai bendera yang menentukan bagaimana aturan ini diwariskan oleh objek turunan.

(Diperoleh dari AuthorizationRule)
IsInherited

Mendapatkan nilai yang menunjukkan apakah aturan ini diatur secara eksplisit atau diwarisi dari objek kontainer induk.

(Diperoleh dari AuthorizationRule)
PropagationFlags

Mendapatkan nilai bendera penyebaran, yang menentukan bagaimana pewarisan aturan ini disebarluaskan ke objek turunan. Properti ini signifikan hanya ketika nilai InheritanceFlags enumerasi bukan None.

(Diperoleh dari AuthorizationRule)

Metode

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk