注册表子项不可见??C#.
本文关键字:注册表 | 更新日期: 2023-09-27 18:30:47
所以我正在尝试在我正在构建的程序中实现一个注册表系统。到目前为止,程序的主程序打开、编辑或删除与此键关联的任何值都没有问题。
但是,我的清理程序确实如此,当我告诉它使用以下方式向我显示所有子项时:
Registry.LocalMachine.OpenSubKey (regappend + reg, true).GetSubKeyNames ();
它告诉我该值不存在或为空,因此我无法从中获取信息。
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);
}
}
}
变量自始至终都是相同的。
我做错了什么?
在我使用以下代码创建密钥后,您的程序对我来说运行良好:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true))
{
key.CreateSubKey("El''Main").SetValue("F", "bar");
}
请注意,在Regedit中查看时,密钥实际上位于(在我的机器上)以下位置:
HKLM'Software'Wow6432Node'El'Main
您没有获得预期值的原因可能是您在注册表中查找了错误的位置。