[英]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.