在 vb.net 2010 'NullReferenceException was unhandled' 中创建注册表项

Creating a Registry Key in vb.net 2010 'NullReferenceException was unhandled'

我在创建注册表项时遇到问题...

这是我的代码:

   Dim KeyName As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"
   Dim Value As String = Application.ExecutablePath
   My.Computer.Registry.CurrentUser.OpenSubKey(KeyName, True).SetValue(Application.ProductName, Value)

这是我的问题:

请帮忙。

问题是您的密钥名称包含 "HKEY_CURRENT_USER\"。 My.Computer.Registry.CurrentUser 已经将您带到 HKEY_CURRENT_USER,因此您不需要将其作为键名的一部分。你的第一个声明应该是

Dim KeyName As String = "Software\Microsoft\Windows\CurrentVersion\Run"