【发布时间】:2012-10-30 19:44:54
【问题描述】:
我想从注册表中读取并设置一些值,但我不断收到 NullReferenceExceptions。
public partial class Form1 : Form
{
RegistryKey rkApp = null;
RegistryKey settings = null;
public Form1()
{
InitializeComponent();
rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
settings = Registry.CurrentUser.OpenSubKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Shit", true);
if (settings.GetValue("automove") != null)
{
automove = true;
autostartToolStripMenuItem.Checked = true;
}
}
}
我在这个例子中删除了一些不相关的代码,但这是我的代码......有什么想法吗?
如果(settings.GetValue("automove") != null),则错误出现在行中
【问题讨论】:
-
从哪一行抛出异常(检查异常中的
StackTrace) -
if (settings.GetValue("automove") != null)
-
settings不是null? -
那么您知道为什么会收到 NullReferenceException - 您的实际问题是为什么设置为空,是吗?上下文对于人们能够快速正确地回答您非常重要。 =)
-
你为什么要从 HKCU 打开 HKLM? msdn.microsoft.com/en-us/library/… 还要检查stackoverflow.com/questions/1074411/…
标签: c#