2015-06-20 17 views
-3

我试图使Launcher_SessionLoginAnimation_OnShow设置为1时填充复选框。 我似乎无法解决导致System.NullReferenceException的问题。 我试图搜索谷歌和stackexchange,但我找不到解决方案。尝试读取注册表项时System.NullReferenceException VisualBasic

Imports Microsoft.Win32 
Public Class Form1 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim subKeyName As String = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ImmersiveShell\\Grid" 
     Dim key As RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(subKeyName) 
     If key IsNot DBNull.Value Then 
      Dim readValue As String = key.GetValue("Launcher_SessionLoginAnimation_OnShow") 
      If readValue.Equals("1") Then 
       CheckBox1.Checked = True 
      End If 
     End If 
    End Sub 

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged 
     If CheckBox1.Checked = True Then 
      Dim key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ImmersiveShell\\Grid", True) 
      If key IsNot Nothing Then 
       key.SetValue("Launcher_SessionLoginAnimation_OnShow", 1) 
       key.Close() 
      End If 
     Else 
      Dim key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ImmersiveShell\\Grid", True) 
      If key IsNot Nothing Then 
       key.SetValue("Launcher_SessionLoginAnimation_OnShow", 0) 
       key.Close() 
      End If 
     End If 
    End Sub 
End Class 

readValue As String = key.GetValue("Launcher_SessionLoginAnimation_OnShow")抛出System.NullReferenceException 谁能帮我解决这个问题?任何帮助表示赞赏。

+0

从MSDN:'返回:与名称相关联的值,或者如果没有找到名称,则返回Nothing。您还应该打开Option Strict。有关NullReferenceException的更多信息,请参见[Visual Basic中的NullReference异常](http://stackoverflow.com/a/26761773/1070452) – Plutonix

+3

使用DBNull没有意义。 –

回答

3

此行是错误的:

If key IsNot DBNull.Value Then 

DBNull.Value用于SQL数据库的结果,而不是注册表项。试试这个:

If key IsNot Nothing Then