2013-08-20 82 views
2

嘗試修復Null值時,我遇到了C#(我是新手)的問題。 因此我有一個變量「verif」(String verif = String.Empty;),我用它從Windows註冊表中讀取一些密鑰。如果密鑰存在,我的代碼可以工作,但是當它沒有出現錯誤「NullReferanceException未處理」時。 我嘗試了幾種方法來捕捉異常,併發出「If」聲明,但我失敗了。 我的代碼是這樣的:在C中打開註冊表項時出現空值錯誤

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 
{... 

任何提示? 非常感謝,Bogdan。

+0

如果您在Visual Studio中以調試模式運行此代碼,那麼哪一行會指示引發未處理的異常?此外,您可能會從Microsoft的C#程序員指南有關[異常和異常處理](http://msdn.microsoft.com/zh-cn/library/ms173160.aspx) – Adrian

+0

的文章中獲得一些價值。錯誤是針對「verif」變量,在行「verif = Registry.CurrentUser.OpenSubKey(@」Software \ TDCredentials「)。GetValue(」user_mdw_「+ tara +」_CC「)。ToString();」並且只有在沒有註冊表鍵的情況下才能滿足需求。例如,有一個值爲「100」的註冊表鍵「JOHN」,我的變量「verif」搜索它。如果「JOHN」存在,作爲註冊表鍵值,則代碼沒有錯誤。如果不是C#引發了線程中提到的錯誤,則未處理的值爲空值。 – BogdanM

回答

3

首先,你需要檢查

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

,這不是空的。然後調用getvalue方法。因爲如果上面的鍵值爲null,那麼下面的getvalue將拋出異常。

3

看看你在做什麼,這條線很可能是你的問題之一;

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) { 
... 
+0

你好Joachim,我用你的想法,但同樣的錯誤:verif = key!= null? key.GetValue(「user_mdw_」+ tara +「_CC」)。ToString():null; – BogdanM

+0

@BogdanM對不起,更新了答案,'ToString()'有同樣的問題。 –

0

嘗試以下檢查以測試是否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(); 

      } 
+0

檢查孔註冊表鍵「... Software \ TDCredentials」是沒有用的,因爲它可能已被其他鍵使用。我必須檢查一個特定的鍵值...所以它沒有幫助 – BogdanM

0

嘗試在OpenSubKey()使用的ToString之前&的GetValue()方法檢查NULL()方法。

0

你試圖在一行中做太多,而不去檢查結果。

首先,正如其他人已經說過的,您需要檢查OpenSubKey是否不返回空值。您還需要確保當你完成的關鍵是封閉的,有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情況: