简体   繁体   English

尝试使用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. 但尝试时会收到NullReferenceException错误。 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);
       }
   }
}

在此处输入图片说明

Try 尝试

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

(double '\\') (双'\\')

If not, try this answer . 如果不是,请尝试此答案

Most likely the nullreference exception is on the RegistryKey rk itself. nullreference异常很可能在RegistryKey rk本身上。

Is your application running as a 32-bit or 64-bit application? 您的应用程序是作为32位还是64位应用程序运行的? You shouldn't need to specify the Wow6432Node part and should just be able to reference @"\\SOFTWARE\\123Test 您不需要指定Wow6432Node部分,而应该能够引用@"\\SOFTWARE\\123Test

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. 如果为null,请检查路径是否有效。 If it is valid do it like this 如果有效,就这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取注册表项时,C#NullReferenceException - C# NullReferenceException when reading a registry key 如何在C#注册表中将权限设置为完全控制 - how to set permissions to full control in c# registry 尝试在C#中通过蓝牙发送字节时发生NullReferenceException - NullReferenceException when trying to send bytes via Bluetooth in C# 尝试创建 C# EF 核心迁移时出错,System.NullReferenceException: Object 引用未设置为 object 的实例 - Error when trying to create a C# EF core migration, System.NullReferenceException: Object reference not set to an instance of an object 如何在C#中获取注册表写入权限 - How to get registry write permissions in C# 尝试使用 C# 在 Windows 注册表中设置 DWORD 值时出错 - Error when trying to set a DWORD value in the windows registry using C # 尝试设置 class 属性时出现 NullReferenceException - NullReferenceException when trying to set class property C#:在类中存储实例; 尝试获取其值时获取NullReferenceException - C#: Storage of instances in the class; get NullReferenceException when trying to get theirs value 设置注册表值时出现NullReferenceException - NullReferenceException when setting a Registry Value 导航时出现 WPF C# NullReferenceException - WPF C# NullReferenceException when navigating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM