我试图从C#中的子项“HKLM \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ SystemRestore”中读取注册表项“RPSessionInterval”。我正在使用下面的代码并得到异常“对象引用未设置为对象的实例”。C#从注册表中获取SystemRestore的状态
string systemRestore = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore";
RegistryKey baseRegistryKey = Registry.LocalMachine;
public string SystemRestoreStatus(string subKey, string keyName)
{
RegistryKey rkSubKey = baseRegistryKey.OpenSubKey(systemRestore);
if (rkSubKey != null)
{
try
{
string sysRestore = rkSubKey.GetValue("RPSessionInterval").ToString();
if (string.Compare(sysRestore, "1") == 0)
{
MessageBox.Show("System Restore is Enabled!");
return "System Restore is Enabled!";
}
else if (string.Compare(sysRestore, "0") == 0)
{
MessageBox.Show("System Restore is Disabled!");
return "System Restore is Disabled!";
}
else
{
return null;
}
}
catch (Exception ex) //This exception is thrown
{
MessageBox.Show("Error while reading registry key: " + subKey + "\\" + keyName + ". ErrorMessage: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
else
{
MessageBox.Show("Error while reading registry key: " + subKey + "\\" + keyName + " does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
这里是一个图片显示该注册表项确实存在:
Are you sure keyName =“RPSessionInterval”? – 2011-12-23 20:44:32
糟糕,是的,忘记在代码中添加它。刚编辑我的代码以反映这些更改。同样的错误,仍然。 – 12hys 2011-12-23 20:47:13
您确定您的应用程序具有读取您尝试访问的密钥所需的权限吗? - 编辑:如果不是那么没关系,会引发一个不同的错误。 – 2011-12-23 20:49:11