windows 关于 Registry CreateSubKey 的简单问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5217442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple question about Registry CreateSubKey
提问by Kasper Hansen
Why doesn't this work? I am trying to create a registry key under [HKEY_LOCAL_MACHINE\SOFTWARE\MyService], but nothing is created.
为什么这不起作用?我正在尝试在 [HKEY_LOCAL_MACHINE\SOFTWARE\MyService] 下创建一个注册表项,但没有创建任何内容。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
namespace RegistryKey
{
class Program
{
static void Main(string[] args)
{
const string SUB_KEY_NAME = @"SOFTWARE\MyService";
// Create a subkey named MyService under HKEY_LOCAL_MACHINE.
Registry.LocalMachine.CreateSubKey(SUB_KEY_NAME);
}
}
}
Update:Never mind. I'm an idiot. I used the Remote registry Editor to inspect the registry in the belief that it would show the same as regedit. It didn't! I can see the path using regedit.
更新:没关系。我是个白痴。我使用远程注册表编辑器检查注册表,相信它会显示与 regedit 相同的内容。它没有!我可以使用regedit查看路径。
回答by David Heffernan
You don't have write access to HKLM. If you want to write here then you need to either:
您没有对 HKLM 的写访问权限。如果你想在这里写,那么你需要:
- run the process as an elevated user, or.
- only attempt to write to HKLM during install.
- 以提升的用户身份运行该进程,或者。
- 仅在安装期间尝试写入 HKLM。
回答by user3733672
Try with this code:
试试这个代码:
RegistryKey regkey = Registry.CurrentUser;
regkey = regkey.CreateSubKey(SUB_KEY_NAME); //this is the path then you create yours keys
regkey.SetValue("Install", "ok"); //name of key exp:(install) and then the value exp:(ok)