我正在創建一個類來處理我的應用程序的註冊表項,但我很早就遇到了一些問題。如何使用'字典'時解決錯誤
在下面,它應該採取一個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