C# 无法在注册表中创建子项
以下代码运行时没有错误,也没有异常。但是,不会创建子键。不知道怎么了。谢谢C# 无法在注册表中创建子项,c#,registry,C#,Registry,以下代码运行时没有错误,也没有异常。但是,不会创建子键。不知道怎么了。谢谢 try { var stupPath = "\"" + folder + "\\My.exe\" \"currentuser\" \"" + folder + "\""; var subkeyHKLM = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Active Setup\Installed Components", true);
try
{
var stupPath = "\"" + folder + "\\My.exe\" \"currentuser\" \"" + folder + "\"";
var subkeyHKLM = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Active Setup\Installed Components", true);
if (subkeyHKLM == null) return;
subkeyHKLM.CreateSubKey("MyAddIn");
subkeyHKLM.SetValue("StubPath", stupPath, RegistryValueKind.ExpandString);
subkeyHKLM.Close();
}
catch (Exception ex)
{
}
更新:
我改为以下内容,现在可以使用,但密钥是在HKLM\Software\Wow6432Node\Microsoft\Active Setup\Installed Components中创建的
var newKey = subkeyHKLM.CreateSubKey("MyAddIn");
if (newKey != null)
{
newKey.SetValue("StubPath", stupPath, RegistryValueKind.ExpandString);
}
subkeyHKLM.Close();
好吧,你正在吞咽所有的异常,所以你无论如何都不会看到任何异常。请注意,如果相关子项不存在,您也将返回。你有没有对代码进行调试,看看它能走多远?我们不知道,但你可以…你检查过你的密钥是否是在WOW64密钥下创建的吗?我通过调试发现没有出现异常,但没有创建密钥。我在WOW6432节点中找到了它,但只有密钥MyAddIn,名称“StubPath”没有创建。真奇怪