嘗試使用 C# 設置註冊表權限時出現 NullReferenceException (NullReferenceException when trying to set registry permissions with C#)


問題描述

嘗試使用 C# 設置註冊表權限時出現 NullReferenceException (NullReferenceException when trying to set registry permissions with C#)

I'm trying to set the below permissions on a registry key. But I get a NullReferenceException error when it tries. Being a novice makes this tuff. Throw in permissions (which have always confused me) and I'm stumped. Can someone tell me why I'm getting this? Thanks.

using System;
using Microsoft.Win32;
using System.Security.AccessControl;

namespace ConsoleApplication7
{
   class Program
   {
       static void Main(string[] args)
       {
           RegistrySecurity rs = new RegistrySecurity();
           string user = "Everyone";

           RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Wow6432Node\123Test", true);

           rs.AddAccessRule(new RegistryAccessRule(user,
               RegistryRights.FullControl | RegistryRights.TakeOwnership,
               InheritanceFlags.ContainerInherit,
               PropagationFlags.None,
               AccessControlType.Allow));

           rk.SetAccessControl(rs);
       }
   }
}


參考解法

方法 1:

Try

@"\\SOFTWARE\\Wow6432Node\\123Test"
  

(double '\')

If not, try this answer.

方法 2:

Most likely the nullreference exception is on the RegistryKey rk itself.

Is your application running as a 32‑bit or 64‑bit application? You shouldn't need to specify the Wow6432Node part and should just be able to reference @"\SOFTWARE\123Test

方法 3:

This line is causing you error

 RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Wow6432Node\123Test", true);

Place a debugger here and see if it has value or it is null. If it is null check that path is valid. If it is valid do it like this

RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"\\SOFTWARE\\Wow6432Node\\123Test", true);

(by JimDelSam LeachFishcakeEhsan)

參考文件

  1. NullReferenceException when trying to set registry permissions with C# (CC BY‑SA 3.0/4.0)

#permissions #registry #C#






相關問題

SharePoint/WSS:修改“創建者”字段? (SharePoint/WSS: Modify "created by" field?)

從 MS Access 訪問 .mdb 文件中的後端表 (Accessing backend tables in .mdb files from MS Access)

如何以編程方式對 AD OU 條目設置“列出內容”和“列出對象”權限? (How can I programmatically set "List Content" and "List Object" permissions on AD OU entries?)

嘗試使用 C# 設置註冊表權限時出現 NullReferenceException (NullReferenceException when trying to set registry permissions with C#)

可執行腳本在 Linux 機器上獲得權限被拒絕 (Executable script gets permission denied on Linux box)

iOS Facebook 令牌權限生日 (iOS Facebook token Permissions birthday)

如何使 644 個權限文件可從 PHP 寫入? (How do I make 644 permission files writable from PHP?)

Android 6.0 中的權限更改回調 (Permission changed callback in Android 6.0)

LINQ和數據庫權限 (LINQ and Database Permissions)

多個用戶可以訪問/更新 Market 中的單個 Android 應用程序嗎? (Can multiple users access/update a single Android app in the Market?)

運行具有權限的 Eclipse 應用程序或小程序 (Running Eclipse aplication or applet with permissions)

通過 AirWatch 卸載 Android APK (Uninstall Android APK via AirWatch)







留言討論