简体   繁体   English

创建注册表项

[英]Create Registry Key

I'm trying to add a registry key (if it doesn't already exist) and have the following code: 我正在尝试添加一个注册表项(如果尚不存在)并具有以下代码:

RegistrySecurity rs = new RegistrySecurity();

rs.AddAccessRule(new RegistryAccessRule(Environment.UserDomainName + "\\" + Environment.UserName,
            RegistryRights.FullControl,
            InheritanceFlags.ObjectInherit,
            PropagationFlags.InheritOnly,
            AccessControlType.Allow));

RegistryKey RK = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION");
if (RK != null)
{

}
else
{
    RegistryKey RK1 = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", RegistryKeyPermissionCheck.ReadWriteSubTree, rs);
    RK1.SetValue("TestValue1", "Test.exe", RegistryValueKind.DWord);
} 

The problem is that I get an UnauthorisedAccessException at the point at the CreateSubKey line. 问题是我在CreateSubKey行的某个点得到了UnauthorisedAccessException。

Sounds to me like a permissions issue, but I thought the RegistrySecurity took care of it. 在我看来,这似乎是一个权限问题,但我认为RegistrySecurity负责解决此问题。

Does anyone know what the problem may be? 有谁知道可能是什么问题?

The AddAccessRule method isn't going to grant you write access permissions to the registry key if your user account doesn't already have write access to that part of the registry (otherwise any user could just grant themselves permissions to any part of the registry they wanted). 如果您的用户帐户尚未对该注册表部分进行写访问,则AddAccessRule方法不会授予您对该注册表项的写访问权限(否则,任何用户都可以向他们授予自己对注册表任何部分的访问权限)通缉)。

You will need to run your application under a user account that already has read/write permissions to that part of the registry for this to work. 您将需要在一个用户帐户下运行您的应用程序,该用户帐户已经对注册表的该部分具有读/写权限,这样才能起作用。 On my Windows 7 machine, that is the SYSTEM account and any user belonging to the Administrators group. 在我的Windows 7计算机上,即SYSTEM帐户和属于Administrators组的任何用户。 The default permissions for those FeatureControl keys grant read access to standard users and deny write access. 这些FeatureControl密钥的默认权限将授予标准用户读取访问权限,并拒绝写入访问权限。

The articles here and here describe how you can go about elevating your application so it has Administrator privileges and consequently write access to those registry keys. 此处此处的文章描述了如何提升应用程序使其具有管理员特权,并因此对那些注册表项具有写访问权。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM