RegistryRights 列舉

定義

指定可套用至登錄物件的存取控制 (Access Control) 權限。

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

C#
[System.Flags]
public enum RegistryRights
繼承
RegistryRights
屬性

欄位

ChangePermissions 262144

變更與登錄機碼相關聯之存取規則和稽核規則的權限。

32

保留供系統使用。

CreateSubKey 4

建立登錄機碼之子機碼的權限。

Delete 65536

刪除登錄機碼的權限。

EnumerateSubKeys 8

列出登錄機碼之子機碼的權限。

ExecuteKey 131097

ReadKey 相同。

FullControl 983103

對登錄機碼執行完全控制以及修改其存取規則和稽核規則的權限。

Notify 16

要求通知登錄機碼變更的權限。

QueryValues 1

查詢登錄機碼中名稱/值組的權限。

ReadKey 131097

查詢登錄機碼中名稱/值組、要求通知變更、列舉其子機碼,以及讀取其存取規則和稽核規則的權限。

ReadPermissions 131072

開啟並複製登錄機碼之存取規則和稽核規則的權限。

SetValue 2

建立、刪除或設定登錄機碼中名稱/值組的權限。

TakeOwnership 524288

變更登錄機碼擁有人的權限。

WriteKey 131078

建立、刪除、設定登錄機碼中名稱/值組、建立或刪除子機碼、要求通知變更、列舉其子機碼,以及讀取其存取規則和稽核規則的權限。

範例

下列程式碼範例示範列舉的使用 RegistryRights 。 程式碼會建立測試金鑰,允許目前的使用者 ReadKey 和 Delete 存取權限,但拒絕 ChangePermissions 和 WriteKey 許可權。 後續嘗試根據這些許可權操作金鑰成功或失敗。

刪除金鑰之前,程式碼會暫停。 您可以切換至登錄編輯程式 (Regedit.exe 或Regedt32.exe) ,並使用登錄編輯程式來存取金鑰時,確認相同的存取權限適用。

如果您從命令列使用 RunAs ,以沒有系統管理員許可權的本機使用者身分執行登錄編輯程式和範例程式碼,此範例最適合使用。 例如,如果您已定義名為 TestUser 的本機使用者,此命令 runas /user:TestUser cmd 會開啟命令視窗,您可以從中執行登錄編輯程式,然後執行範例程式碼。

C#
using System;
using System.Reflection;
using System.Security;
using System.Security.AccessControl;
using Microsoft.Win32;

public class Example
{
    public static void Main()
    {
        // Delete the example key if it exists.
        try
        {
            Registry.CurrentUser.DeleteSubKey("RegistryRightsExample");
            Console.WriteLine("Example key has been deleted.");
        }
        catch (ArgumentException)
        {
            // ArgumentException is thrown if the key does not exist. In
            // this case, there is no reason to display a message.
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to delete the example key: {0}", ex);
            return;
        }

        string user = Environment.UserDomainName + "\\" + Environment.UserName;

        RegistrySecurity rs = new RegistrySecurity();

        // Allow the current user to read and delete the key.
        //
        rs.AddAccessRule(new RegistryAccessRule(user,
            RegistryRights.ReadKey | RegistryRights.Delete,
            InheritanceFlags.None,
            PropagationFlags.None,
            AccessControlType.Allow));

        // Prevent the current user from writing or changing the
        // permission set of the key. Note that if Delete permission
        // were not allowed in the previous access rule, denying
        // WriteKey permission would prevent the user from deleting the
        // key.
        rs.AddAccessRule(new RegistryAccessRule(user,
            RegistryRights.WriteKey | RegistryRights.ChangePermissions,
            InheritanceFlags.None,
            PropagationFlags.None,
            AccessControlType.Deny));

        // Create the example key with registry security.
        RegistryKey rk = null;
        try
        {
            rk = Registry.CurrentUser.CreateSubKey("RegistryRightsExample",
                RegistryKeyPermissionCheck.Default, rs);
            Console.WriteLine("\r\nExample key created.");
            rk.SetValue("ValueName", "StringValue");
        }
        catch (Exception ex)
        {
            Console.WriteLine("\r\nUnable to create the example key: {0}", ex);
        }
        if (rk != null) rk.Close();

        rk = Registry.CurrentUser;

        RegistryKey rk2;

        // Open the key with read access.
        rk2 = rk.OpenSubKey("RegistryRightsExample", false);
        Console.WriteLine("\r\nRetrieved value: {0}", rk2.GetValue("ValueName"));
        rk2.Close();

        // Attempt to open the key with write access.
        try
        {
            rk2 = rk.OpenSubKey("RegistryRightsExample", true);
        }
        catch (SecurityException ex)
        {
            Console.WriteLine("\nUnable to write to the example key." +
                " Caught SecurityException: {0}", ex.Message);
        }
        if (rk2 != null) rk2.Close();

        // Attempt to change permissions for the key.
        try
        {
            rs = new RegistrySecurity();
            rs.AddAccessRule(new RegistryAccessRule(user,
                RegistryRights.WriteKey,
                InheritanceFlags.None,
                PropagationFlags.None,
                AccessControlType.Allow));
            rk2 = rk.OpenSubKey("RegistryRightsExample", false);
            rk2.SetAccessControl(rs);
            Console.WriteLine("\r\nExample key permissions were changed.");
        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine("\nUnable to change permissions for the example key." +
                " Caught UnauthorizedAccessException: {0}", ex.Message);
        }
        if (rk2 != null) rk2.Close();

        Console.WriteLine("\r\nPress Enter to delete the example key.");
        Console.ReadLine();

        try
        {
            rk.DeleteSubKey("RegistryRightsExample");
            Console.WriteLine("Example key was deleted.");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Unable to delete the example key: {0}", ex);
        }

        rk.Close();
    }
}

/* This code example produces the following output:

Example key created.

Retrieved value: StringValue

Unable to write to the example key. Caught SecurityException: Requested registry access is not allowed.

Unable to change permissions for the example key. Caught UnauthorizedAccessException: Cannot write to the registry key.

Press Enter to delete the example key.

Example key was deleted.
 */

備註

當您建立 RegistrySecurity 物件時, RegistryRights 請使用 列舉來指定登錄存取權限。 若要將存取權限套用至登錄機碼,請先將物件新增 RegistryAccessRule 至 物件,然後使用 方法或方法的適當多載 RegistryKey.CreateSubKey ,將物件附加 RegistrySecurity 至機碼 RegistryKey.SetAccessControl RegistrySecurity

適用於

產品 版本
.NET Core 1.0, Core 1.1, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5

另請參閱