'你调用的对象是空的。'从VB.NET中的注册表中读取时 [英] 'Object reference not set to an instance of an object.' when reading from the registry in VB.NET
问题描述
您好,我正在尝试从Visual Basic中的regedit键读取一个简单值。我希望程序读取密钥,然后将注册表值设置为该密钥的值。这是代码:
Hello, I'm trying to read a simple value from a regedit key in Visual Basic. I want the program to read the key, then set a registry value as the value of that key. Here's the code:
Shared Sub DWMDefault()
Dim regKey As RegistryKey = _
My.Computer.Registry.LocalMachine.OpenSubKey("HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM", True)
Dim ColorizationColor As Decimal = regKey.GetValue("ColorizationColor")
regKey.SetValue("AccentColor", Convert.ToDecimal(ColorizationColor))
regKey.SetValue("ColorizationColor", Convert.ToDecimal(ColorizationColor))
regKey.Close()
End Sub
它只是吐出错误,如包含
的行中的标题所示
It just spits out the error as seen in the title on the line that contains
Dim ColorizationColor As Decimal = regKey.GetValue("ColorizationColor")
请帮助,我不知道如何解决这个问题,也许只是觉得我犯了一些愚蠢的错误。希望就是这样,谢谢!
我尝试过:
我尝试将Colorization颜色作为一个字节和一个registryKey返回,但这些都没有。
Please help, I have no idea on what to do to fix this issue and maybe just thought I was making some silly mistake. Hopefully that's the case, thanks!
What I have tried:
I've tried returning the Colorization color as a byte and a registryKey, but neither of those worked.
推荐答案
你的代码说从localmachine开始并打开一个子项。但是,您为当前用户提供了完整的路径。这是有效的:
Your code says to start at localmachine and open a subkey. But you give a full path to current user. This works:
Dim regKey As RegistryKey =
My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\DWM", False)
Dim ColorizationColor As Decimal = regKey.GetValue("ColorizationColor")
regKey.SetValue("AccentColor", Convert.ToDecimal(ColorizationColor))
regKey.SetValue("ColorizationColor", Convert.ToDecimal(ColorizationColor))
regKey.Close()
错误意味着什么它说,你的regKey值是Nothing
The error meant what it said, your regKey value was Nothing
这篇关于'你调用的对象是空的。'从VB.NET中的注册表中读取时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!