我正在创建一个类来处理我的应用程序的注册表项,但我很早就遇到了一些问题。如何使用'字典'时解决错误
在下面,它应该采取一个SubKey的所有键/值,并将它们添加到Dictonary
。被注释掉的消息框正确显示了键和值,但每次运行该函数时,下面的行都会生成一个错误A first chance exception of type 'System.NullReferenceException'
。
由于注册表项本身似乎很好,我认为这是与我使用RegKeys Dictonary
的方式有关。如果有人可以看一看,并建议我会很感激。谢谢。
这是我如何启动类(我还没有尝试过做别的事,只是还没有) -
Private Sub getMyRegSettings()
Dim ServerPing As RegistryKey = Registry.CurrentUser.OpenSubKey("ServerPing")
Dim Servers As RegistryKey = ServerPing.OpenSubKey("Servers")
Dim MyRegistry As New MyRegistry(Servers)
Dim RegKeys As Dictionary(Of String, String) = MyRegistry.RegKeys
End Sub
这里是我造成了一些麻烦类 -
Public Class MyRegistry
Public RegKeys As Dictionary(Of String, String)
Public Sub New(SubKey As RegistryKey)
get_registry_keys(SubKey)
End Sub
Private Sub get_registry_keys(SubKey As RegistryKey)
' Print the information from the Test9999 subkey.
For Each valueName As String In SubKey.GetValueNames() ' Error occurs here
'MsgBox("Key: " & valueName & vbCrLf & "Value: " & SubKey.GetValue(valueName))
RegKeys(valueName) = SubKey.GetValue(valueName).ToString()
Next valueName
End Sub
End Class
你在哪里创建一个新的RegKeys实例里面MyRegistry?在构造函数中,您不会在get_registry_keys中使用它,当它仍然为空时。 – 2013-04-08 15:37:47
不会将其声明为类的属性创建它吗?我直接在'Public Class MyRegistry'下做了这个。谢谢。 – 2013-04-08 15:39:50