注册表安全设置

时间:2010-08-23 14:14:08

标签: c# .net registry

我正在尝试访问注册表的某个位,但是当我尝试打开它时它会一直返回null。

但我知道位置是正确的,因为我可以在reedit中导航到它。

这是我尝试访问它的代码行。

Microsoft.Win32.RegistryKey RK = Microsoft.Win32.Registry.LocalMachine.OpenSubK("Software\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData", false);

任何人都有任何想法?

更新

看起来我看到了这个注册表的不同版本,这需要是一个32位的应用程序,但它需要能够看到32位和64位版本的注册表。

谢谢

3 个答案:

答案 0 :(得分:0)

您是否通过属性[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, Flags = System.Security.Permissions.SecurityPermissionFlag.RegistryPermission)]应用了访问注册表项的权限,或者您是否以管理员身份运行该程序?以下是MSDN详细介绍安全权限,更恰当地RegistryPermission

答案 1 :(得分:0)

您的应用程序如何运行?换句话说,您的应用程序在什么上下文中运行?它是通过双击.exe运行还是启动进程的安装应用程序?安装应用程序在不同于您期望的上下文中运行。

答案 2 :(得分:0)

 var tempKey = Registry.LocalMachine;

和安全访问

var rule = new RegistryAccessRule(LOGON_USER_NAME, RegistryRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
var security = new RegistrySecurity();
security.AddAccessRule(rule);
tempKey.SetAccessControl(security);

 tempKey = tempkey.OpenSubKey(SUB_KEY, RegistryKeyPermissionCheck.ReadWriteSubTree);

和LOGON_USER_NAME

1)

var LOGON_USER_NAME = Environment.UserName;

2)

var LOGON_USER_NAME = System.Security.Principal.WindowsIdentity.GetCurrent().Name;