在C#中打开注册表项时出现Null值错误

本文关键字:Null 错误 注册表 | 更新日期: 2023-09-27 18:28:14

我在尝试修复Null值时遇到C#问题(我是新手)。因此,我有一个变量"verif"(String-verif=String.Empty;),我用它从Windows注册表中读取一些键。如果键存在,我的代码就会工作,但当它不存在时,我会得到错误"NullReferenceException未处理"。我尝试了几种方法,试图抓住例外,提出一个"如果"语句,但我失败得很惨。我的代码是这样的:

RegistryKey key_user;
RegistryKey key_pwd;
String code = String.Empty;
String tara = String.Empty;
String tot = String.Empty;
String pwd_mdw = String.Empty;
String user_mdw = String.Empty;
String user_uca = String.Empty;
String pwd_uca = String.Empty;
String verif = String.Empty;     
private void button1_Click(object sender, EventArgs e)
{tot = listBox1.SelectedValue.ToString();
//MessageBox.Show(tot);
tara = tot.Substring(tot.Length - 2, 2);
//MessageBox.Show(tara);
code = listBox1.SelectedValue.ToString().Substring(0, 2);
user_mdw = textBox1.Text;
//MessageBox.Show(user_mdw);
pwd_mdw = textBox2.Text;
//MessageBox.Show(pwd_mdw);        
if (code == "CC")
{
verif = Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials").GetValue("user_mdw_" + tara + "_CC").ToString();
MessageBox.Show("Verif",verif);
MessageBox.Show(user_mdw, "user_mdw");
if (verif==null)
{
key_user = Registry.CurrentUser.CreateSubKey("Software''TDCredentials");
key_user.SetValue("user_mdw_" + tara + "_CC", user_mdw);
key_user.Close();
key_pwd = Registry.CurrentUser.CreateSubKey("Software''TDCredentials");
key_pwd.SetValue("pass_mdw_" + tara + "_CC", pwd_mdw);
key_pwd.Close();
MessageBox.Show("User and Password inserted successfully!");
textBox1.Clear();
textBox2.Clear();
}
else
{...

有什么提示吗?非常感谢,博格丹。

在C#中打开注册表项时出现Null值错误

看看你正在尝试做什么,这一行很可能是你的问题之一;

verif = Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials")
            .GetValue("user_mdw_" + tara + "_CC").ToString();

如果密钥不存在,OpenSubKey将返回null,并且您在不检查的情况下对其调用GetValue()

您可以更改行以添加支票,类似于;

var key = Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials");
var value = key != null ? key.GetValue("user_mdw_" + tara + "_CC") : null;
verif = value != null ? value.ToString() : null;
if(verif == null) { 
...

首先需要检查

Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials")

这不是空的。然后调用getvalue方法。Beause如果上面的键为null,那么下面的getvalue将抛出异常。

尝试以下检查以测试Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials")是否不是null,否则它将轰炸:

 if (code == "CC")
        {
            if (Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials") != null)
            {
                verif =
                    Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials").GetValue("user_mdw_" + "Test" + "_CC").
                        ToString();
            }

尝试在OpenSubKey()上检查NULL;GetValue()方法,然后再使用ToString()方法。

您试图在一行中做太多的工作,而不在执行过程中检查结果。

首先,正如其他人已经说过的,您需要检查OpenSubKey是否返回null。您还需要使用using语句:来确保完成时密钥已关闭

using (var key = Registry.CurrentUser.OpenSubKey(@"Software'TDCredentials"))
{
    if (key == null)
    {
        // Couldn't open the key, now what?
        // You need to make a decision here.
        // If you read the documentation for CreateSubKey,
        // you'll see that it can *also* return null, so don't rely on it.
    }
    else
    {
        // OK, opened the key, and the using statement will close it.
        // Now we can try reading values. See the next part of the answer.
    }
}

如果成功打开密钥,则可以尝试读取该值。即使成功打开密钥,该值也可能不存在,或者可能不是字符串(例如,它可能是DWORD或二进制值)。

  • 如果该值不存在,GetValue将返回null,因此在不检查的情况下调用ToString将抛出NullReferenceException
  • 即使该值存在,对其调用ToString也是错误的操作,因为它可能不是注册表中的字符串值。例如,如果它是一个二进制值,那么对它调用ToString会得到字符串System.Byte[]。你需要检查它是否真的是一个字符串
else
{
    // OK, opened the key, and the using statement will close it.
    // Now we can try reading values.
    string verif = key.GetValue("user_mdw_" + tara + "_CC") as string;
    if (verif == null)
    {
        // The value does not exist, or is not the type you expected it to be (string).
        // Now what? You need to make a decision here.
    }
    else
    {
        // OK, do something with verif.
    }
}

确保阅读这些方法的文档,并处理它们提到的特殊情况,特别是它们返回null的情况:

  • 创建子密钥
  • OpenSubKey
  • GetValue