所以我试图将一个注册表系统实现到我正在构建的程序中。到目前为止,程序的主要部分打开,编辑或删除与此密钥相关的任何值都没有问题。
然而,我的清洁程序确实如此,当我告诉它使用以下内容向我显示所有子键时:
Registry.LocalMachine.OpenSubKey (regappend + reg, true).GetSubKeyNames ();
它告诉我该值不存在,或者为null,因此我无法从中获取信息。
private static readonly string regappend = "SOFTWARE\\";
private static string reg = "EL\\Main";
// Malfunctioning code:
using (RegistryKey myKey = Registry.LocalMachine.OpenSubKey (regappend + reg, true))
{
if (myKey != null)
{
foreach (string s in myKey.GetValueNames())
{
Console.WriteLine (s);
}
Console.WriteLine (myKey + "\n" + myKey.GetValue ("F") + "\n");
}
}
// Working Code:
using (RegistryKey myKey = Registry.LocalMachine.OpenSubKey (regappend + reg, true))
{
if (myKey.GetValue ("F") != null)
{
f = (string)myKey.GetValue ("F");
if (debugmode == true)
{
Console.WriteLine (f);
}
}
}
变量在整个过程中都是相同的。
我做错了什么?
答案 0 :(得分:1)
使用此代码创建密钥后,您的程序运行正常:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true))
{
key.CreateSubKey("El\\Main").SetValue("F", "bar");
}
请注意,在Regedit中查看时,密钥实际位于(在我的计算机上)下面:
HKLM\Software\Wow6432Node\El\Main
您可能无法获得预期价值的原因是您在注册表中找错了位置。