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
屬性

欄位

名稱 Description
ListDirectory 1

規定有權閱讀目錄內容。

ReadData 1

規定開啟及複製檔案或資料夾的權利。 這不包含讀取檔案系統屬性、擴展檔案系統屬性,或存取與稽核規則的權利。

CreateFiles 2

規定建立檔案的權利。 這項權利需要價值。Synchronize

WriteData 2

規定開啟及寫入檔案或資料夾的權利。 這不包括開啟與寫入檔案系統屬性、擴展檔案系統屬性,或存取與稽核規則的權利。

AppendData 4

規定有權在檔案末尾附加資料。

CreateDirectories 4

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

ReadExtendedAttributes 8

規定有權從資料夾或檔案開啟並複製擴展檔案系統屬性。 例如,此值指定了查看作者及內容資訊的權利。 這不包括讀取資料、檔案系統屬性或存取與稽核規則的權利。

WriteExtendedAttributes 16

規定有權開啟並寫入擴充檔案系統屬性至資料夾或檔案。 這不包括撰寫資料、屬性或存取與稽核規則的能力。

ExecuteFile 32

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

Traverse 32

規定列出資料夾內容及執行該資料夾內應用程式的權利。

DeleteSubdirectoriesAndFiles 64

規定刪除資料夾及該資料夾內任何檔案的權利。

ReadAttributes 128

規定有權從資料夾或檔案中開啟及複製檔案系統屬性。 例如,這個值指定了查看檔案建立或修改日期的權利。 這不包括讀取資料的權利、擴充檔案系統屬性,或存取與稽核規則。

WriteAttributes 256

規定有權開啟並寫入資料夾或檔案的檔案系統屬性。 這不包括撰寫資料、擴充屬性,或存取與稽核規則的能力。

Write 278

規定建立資料夾與檔案,以及新增或移除檔案資料的權利。 這個權利包括 WriteData 權利、權利、 AppendDataWriteExtendedAttributesWriteAttributes 權利和權利。

Delete 65536

規定刪除資料夾或檔案的權利。

ReadPermissions 131072

規定從資料夾或檔案開啟並複製存取與稽核規則的權利。 這不包含讀取資料、檔案系統屬性及擴展檔案系統屬性的權利。

Read 131209

規定有權以唯讀方式開啟及複製資料夾或檔案。 這個權利包括 ReadData 權利、權利、 ReadExtendedAttributesReadAttributesReadPermissions 權利和權利。

ReadAndExecute 131241

規定有權以唯讀方式開啟及複製資料夾或檔案,並執行應用程式檔案。 這個權利包括 Read 權利和 ExecuteFile 權利。

Modify 197055

規定有權讀取、寫入、列出資料夾內容、刪除資料夾與檔案,以及執行應用程式檔案。 這個權利包括 ReadAndExecute 右、右 Write 、右 Delete

ChangePermissions 262144

規定變更檔案或資料夾相關安全與稽核規則的權利。

TakeOwnership 524288

規定變更資料夾或檔案擁有者的權利。 請注意,資源擁有者擁有該資源的完全存取權。

Synchronize 1048576

指定應用程式是否能等待檔案句柄與 I/O 操作完成同步。 當允許存取時,這個值會自動設定,拒絕存取時會自動排除。

FullControl 2032127

規定對資料夾或檔案行使完全控制權,以及修改存取控制與稽核規則的權利。 這個值代表對檔案做任何事的權利,是本列舉中所有權利的組合。

範例

以下範例利用 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 舉會指定特定使用者帳號允許哪些檔案系統動作,以及哪些檔案系統動作會被稽核。

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

此列舉包含數個細緻的系統權利值,以及若干這些細緻值的組合。 使用組合 FullControl值如 、 ReadWrite和 ,比起分別指定每個成分值更為簡單。

權利CreateDirectoriesCreateFiles需要權利Synchronize。 如果你在建立檔案或目錄時沒有明確設定這個 Synchronize 值,它會自動幫你設定。

適用於