在 vb.net 的注册表中写入 sbkey

发布于 2024-09-26 14:13:22 字数 549 浏览 0 评论 0原文

我使用以下代码打开子项,然后在注册表中写入子项

Private Sub RegSubKeyCreates(ByVal sKeyPath As String)
      Dim RegKey As RegistryKey
      Dim SubKeyParam() As String = Nothing

      SubKeyParam = Split(sKeyPath, "\")
      RegKey = Registry.CurrentUser.OpenSubKey(SubKeyParam(0), True).OpenSubKey(SubKeyParam(1), True)
      RegKey.CreateSubKey(SubKeyParam(0)).CreateSubKey(SubKeyParam(1))

End Sub

当我尝试创建子项时,出现错误

System.NullReferenceException 未将对象引用设置为对象的实例

我无法理解发生了什么。有人可以在这方面帮助我吗?

I'm using the following code to open and then write a subkey in the registry

Private Sub RegSubKeyCreates(ByVal sKeyPath As String)
      Dim RegKey As RegistryKey
      Dim SubKeyParam() As String = Nothing

      SubKeyParam = Split(sKeyPath, "\")
      RegKey = Registry.CurrentUser.OpenSubKey(SubKeyParam(0), True).OpenSubKey(SubKeyParam(1), True)
      RegKey.CreateSubKey(SubKeyParam(0)).CreateSubKey(SubKeyParam(1))

End Sub

When I'm trying to create the subkey I'm geting the error

System.NullReferenceException Object reference not set to an instance of an object

I can't understand what is going on. Is there some one to assist me on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-10-03 14:13:22

非常感谢任何试图发送答复的人。
但我自己找到了解决方案。
所以我把它放在这里以防其他人想看。
解决方案是使用注册表 Hive 的心态。
需要明白的是,子键是一一创建的,只有在我们打开注册表到最后一个存在的子键之后。
新的子键不需要像这样在“subkey”之前或之后有任何字符
请看代码。

Private Sub RegSubKeyCreates(ByVal sKeyPath As String)
      Dim RegKey As RegistryKey
      Dim SubKeyParam() As String = Nothing
      Dim UB As Integer
      Dim Bound As Integer
      Dim KeyPath As String

      SubKeyParam = Split(sKeyPath, "\")
      UB = UBound(SubKeyParam)
      For Bound = 0 To UB - 1
           KeyPath = KeyPath + SubKeyParam(Bound) + "\"
      Next
      RegKey = Registry.CurrentUser.OpenSubKey(KeyPath, True)
      RegKey.CreateSubKey(SubKeyParam(UB))

 End Sub

领先的潜艇是:

Select Case RegKeyExists("Software\sKey1")
           Case True
           Case False
                RegSubKeyCreates("Software\skey1")  
                Select Case RegKeyExists("Software\sKey1\sKey2")
                     Case True
                     Case False
                          RegSubKeyCreates("Software\sKey1\sKey2")
                          Select Case RegKeyExists("Software\sKey1\sKey2\sKey3")
                               Case True
                               Case False
                                    RegSubKeyCreates("Software\sKey1\sKey2\sKey3")
                                    Select Case RegKeyExists("Software\skey1\sKey2\sKey3")
                                         Case True
                                              SetRegKeyValue("KeyName", "KeyValue", "TheTotalSubKeyPath")
                                         Case False

                                    End Select
                          End Select

Thank you very much for any one trying to send an answer.
But i found the solution my self.
So i put it here in case that someone else want to see it.
The solution is in the mentality of working with registry Hive.
Needs to understand that the sub keys creates one by one, only after we have open the registry to the last existent sub key.
The new sub key don't need to have any character before or after "subkey" like that
please look at the code.

Private Sub RegSubKeyCreates(ByVal sKeyPath As String)
      Dim RegKey As RegistryKey
      Dim SubKeyParam() As String = Nothing
      Dim UB As Integer
      Dim Bound As Integer
      Dim KeyPath As String

      SubKeyParam = Split(sKeyPath, "\")
      UB = UBound(SubKeyParam)
      For Bound = 0 To UB - 1
           KeyPath = KeyPath + SubKeyParam(Bound) + "\"
      Next
      RegKey = Registry.CurrentUser.OpenSubKey(KeyPath, True)
      RegKey.CreateSubKey(SubKeyParam(UB))

 End Sub

And the sub which leads one this is:

Select Case RegKeyExists("Software\sKey1")
           Case True
           Case False
                RegSubKeyCreates("Software\skey1")  
                Select Case RegKeyExists("Software\sKey1\sKey2")
                     Case True
                     Case False
                          RegSubKeyCreates("Software\sKey1\sKey2")
                          Select Case RegKeyExists("Software\sKey1\sKey2\sKey3")
                               Case True
                               Case False
                                    RegSubKeyCreates("Software\sKey1\sKey2\sKey3")
                                    Select Case RegKeyExists("Software\skey1\sKey2\sKey3")
                                         Case True
                                              SetRegKeyValue("KeyName", "KeyValue", "TheTotalSubKeyPath")
                                         Case False

                                    End Select
                          End Select
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文