当我尝试将值设置为注册表项时,出现 NullReference 异常。下面是我的代码。有谁知道为什么吗?
using System;
using Microsoft.Win32;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\Current Version\\Policies\\System", true);
myKey.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
}
}
}
最佳答案
尝试使用 CreateSubKey 而不是 OpenSubKey。如果 key 不存在,后者将返回 null。 key 很可能不存在,这就是为什么您在下一行中遇到空引用的原因。
关于c# - 设置注册表值时出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/604998/