C#在注册表本地计算机中创建值

Ang*_*.47 22 c# registry

以下代码对我不起作用:

public bool createRegistry()
{
    if (!registryExists())
    {
        Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\\xelo\\");

        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\xelo").SetValue("hostname", (string)hostname, Microsoft.Win32.RegistryValueKind.String);


        return true;
    }
    else
    {
        return updateRegistry();
    }
}
Run Code Online (Sandbox Code Playgroud)

例外:

System.UnauthorizedAccessException | "无法写入注册表项"

Kyl*_*ons 27

非管理员和非管理员管理员用户无权修改HKEY_LOCAL_MACHINE键.以管理员身份运行程序.

  • 尝试将true传递给OpenSubKey调用的第二个参数或使用CreateSubKey的返回值.http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.opensubkey.aspx http://msdn.microsoft.com/en-us/library/ad51f2dx%28v=VS.100%29的.aspx (13认同)
  • 我是管理员,以管理员身份运行给我同样的错误 (4认同)
  • 谢谢凯尔,您必须执行 OpenSubKey("KeyName", true) 其中 true 表示可写:D,编辑您的答案以包括 (2认同)

not*_*ary 9

即使是管理员,我也不认为你可以在LocalMachine上创建新的密钥.确保你这样做

Registry.LocalMachine.CreateSubKey(@"SOFTWARE\YourCompanyName\SomeNewKey");
Run Code Online (Sandbox Code Playgroud)

并不是

Registry.LocalMachine.CreateSubKey("SomeNewKey");
Run Code Online (Sandbox Code Playgroud)


Din*_*eer 9

下面的代码在注册表中创建密钥.

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\\Wow6432Node\\Names");
key.SetValue("Name", "Isabella");
key.Close();
Run Code Online (Sandbox Code Playgroud)

  • 我收到了一条"附加信息:运行代码段时,访问注册表项'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Names'被拒绝.请指教. (2认同)