2013-12-20 80 views
2

我有一個winform,它將註冊數據保存到註冊表下的「Ddoe」 - 但是我需要檢查這個值是否以前已經設置(創建)這樣做,我試圖做:vb.net檢查是否存在註冊表子項值

If (Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HKEY_CURRENT_USER\Software\FCRA\Assignment").GetValue("Ddoe") Is Nothing Then 
'doesnt exist 
else 
'exists 
end if 

然而,這並不正常

我收到一個錯誤

An unhandled exception of type 'System.NullReferenceException' occurred in #appname 

它顯示在這個錯誤的if語句行

是否有更好的方法來檢查此註冊表項的存在,還是我這一切錯誤

回答

5

我認爲問題是,「HKEY_CURRENT_USER \ SOFTWARE \ FCRA \分配」不存在,所以OpenSubKey返回Nothing。您無法在Nothing值上使用任何方法。試試這個:

If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\FCRA\Assignment", 
           "Ddoe", Nothing) Is Nothing Then 
    'value does not exist 
End If 

從這裏摘自:

+1

功能太好了 - 謝謝 –