因此,我尝试将注册表系统实施到正在构建的程序中。到目前为止,程序的主程序在打开,编辑或删除与此键关联的任何值时都没有问题。
但是,我的清理程序会执行此操作,并且当我告诉它使用以下命令向我显示所有子项时:
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);
}
}
}
变量始终相同。
我究竟做错了什么?
使用以下代码创建密钥后,您的程序对我来说运行良好:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true))
{
key.CreateSubKey("El\\Main").SetValue("F", "bar");
}
请注意,在Regedit中查看时,密钥实际上位于(在我的机器上)以下位置:
HKLM\Software\Wow6432Node\El\Main
您未获得期望值的原因可能是您在注册表中查找了错误的位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句