共用方式為


FileSystemRights 列舉

定義

定義建立存取和稽核規則時要使用的訪問許可權。

此列舉支援其成員值的位元組合。

public enum class FileSystemRights
[System.Flags]
public enum FileSystemRights
[System.Flags]
[System.Security.SecurityCritical]
public enum FileSystemRights
[<System.Flags>]
type FileSystemRights = 
[<System.Flags>]
[<System.Security.SecurityCritical>]
type FileSystemRights = 
Public Enum FileSystemRights
繼承
FileSystemRights
屬性

欄位

AppendData 4

指定將數據附加至檔案結尾的權利。

ChangePermissions 262144

指定變更與檔案或資料夾相關聯之安全性和稽核規則的許可權。

CreateDirectories 4

指定建立資料夾的權限 此許可權需要 Synchronize 值。

CreateFiles 2

指定建立檔案的權利。 此許可權需要 Synchronize 值。

Delete 65536

指定刪除資料夾或檔案的許可權。

DeleteSubdirectoriesAndFiles 64

指定刪除資料夾的許可權,以及該資料夾內包含的任何檔案。

ExecuteFile 32

指定執行應用程式檔案的權利。

FullControl 2032127

指定對資料夾或檔案執行完整控制的許可權,以及修改存取控制和稽核規則。 這個值代表使用檔案執行任何動作的許可權,而且是這個列舉中所有許可權的組合。

ListDirectory 1

指定讀取目錄內容的權利。

Modify 197055

指定讀取、寫入、列出資料夾內容、刪除資料夾和檔案,以及執行應用程式檔案的許可權。 此許可權包括 ReadAndExecute 許可權、Write 許可權和 Delete 許可權。

Read 131209

指定開啟資料夾或檔案並複製為唯讀許可權。 此許可權包括 ReadData 許可權、ReadExtendedAttributes 右、ReadAttributes 右和 ReadPermissions 許可權。

ReadAndExecute 131241

指定以唯讀方式開啟和複製資料夾或檔案,以及執行應用程式檔的權利。 此許可權包括 Read 許可權和 ExecuteFile 許可權。

ReadAttributes 128

指定從資料夾或檔案開啟和複製檔案系統屬性的許可權。 例如,這個值會指定檢視檔案建立或修改日期的權利。 這不包括讀取數據、擴充文件系統屬性或存取和稽核規則的許可權。

ReadData 1

指定開啟和複製檔案或資料夾的許可權。 這不包括讀取文件系統屬性、擴充文件系統屬性或存取和稽核規則的許可權。

ReadExtendedAttributes 8

指定從資料夾或檔案開啟和複製擴充檔案系統屬性的許可權。 例如,這個值會指定檢視作者和內容信息的權利。 這不包括讀取數據、文件系統屬性或存取和稽核規則的許可權。

ReadPermissions 131072

指定從資料夾或檔案開啟和複製存取和稽核規則的許可權。 這不包括讀取數據、檔案系統屬性和擴充檔系統屬性的許可權。

Synchronize 1048576

指定應用程式是否可以等候檔句柄與 I/O 作業完成同步處理。 當允許存取時,會自動設定此值,並在拒絕存取時自動排除。

TakeOwnership 524288

指定變更資料夾或檔案擁有者的許可權。 請注意,資源的擁有者具有該資源的完整存取權。

Traverse 32

指定列出資料夾內容的許可權,並執行該資料夾內含的應用程式。

Write 278

指定建立資料夾和檔案,以及從檔案新增或移除數據的權利。 此許可權包括 WriteData 許可權、AppendData 右、WriteExtendedAttributes 右和 WriteAttributes 許可權。

WriteAttributes 256

指定開啟檔案系統屬性並將其寫入資料夾或檔案的許可權。 這不包括寫入數據、擴充屬性或存取和稽核規則的能力。

WriteData 2

指定開啟和寫入檔案或資料夾的許可權。 這不包括開啟和寫入文件系統屬性、擴充文件系統屬性或存取和稽核規則的許可權。

WriteExtendedAttributes 16

指定開啟和寫入擴充檔案系統屬性至資料夾或檔案的許可權。 這不包括寫入數據、屬性或存取和稽核規則的能力。

範例

下列範例會使用 FileSystemRights 列舉來指定存取規則,然後從檔案中移除存取規則。 您必須提供有效的使用者或組帳戶,才能執行此範例。

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)
        {
            FileInfo fileInfo = new(fileName);
            FileSecurity fSecurity = fileInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            fileInfo.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)
        {
            FileInfo fileInfo = new(fileName);
            FileSecurity fSecurity = fileInfo.GetAccessControl();

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            fileInfo.SetAccessControl(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)

        Dim fileInfo As New FileInfo(fileName)
        Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        Dim accessRule As New FileSystemAccessRule(account, rights, controlType)

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        fileInfo.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)

        Dim fileInfo As New FileInfo(fileName)
        Dim fSecurity As FileSecurity = fileInfo.GetAccessControl()

        ' Remove the FileSystemAccessRule from the security settings. 
        fSecurity.RemoveAccessRule(New FileSystemAccessRule(account,
            rights, controlType))

        ' Set the new access settings.
        fileInfo.SetAccessControl(fSecurity)

    End Sub
End Module

備註

FileSystemRights 列舉會指定特定使用者帳戶允許哪些文件系統動作,以及特定使用者帳戶的檔系統動作會稽核哪些文件系統動作。

使用 FileSystemAccessRule 類別建立存取規則,或使用 FileSystemAuditRule 類別建立稽核規則時,請使用 FileSystemRights 列舉。

此列舉包含數個細微的系統許可權值,以及這些細微值組合的數個值。 使用組合值比較容易,例如 FullControlReadWrite,而不是個別指定每個元件值。

CreateDirectoriesCreateFiles 許可權需要 Synchronize 許可權。 如果您在建立檔案或目錄時未明確設定 Synchronize 值,系統會自動為您設定該值。

適用於