更改 VB.NET 中的注册表值 [英] Change a registry value in VB.NET

查看:29
本文介绍了更改 VB.NET 中的注册表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了大量搜索以找到解决问题的方法,但找不到任何具体的解决方案.

I have searched a lot to find a solution for my problem, but I can't find any specific solution.

我需要更改存储在 HKEY_LOCAL_MACHINE\SOFTWARE\APP_NAME 下的值.无法使用 .NET 的注册表功能,因为它们有严格的限制.

I need to change a value which is stored under HKEY_LOCAL_MACHINE\SOFTWARE\APP_NAME. It is not possible to use .NET's registry functions, since they have drastic restrictions.

以下代码段应该可以解决我的问题:

The following snippet should solve my problem:

Dim regKey As RegistryKey
Dim ver As Decimal
regKey = My.Computer.Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\APP_NAME", True)

regKey.SetValue("IP", "192.168.10.15:18500")regKey.Close()

regKey.SetValue("IP", "192.168.10.15:18500") regKey.Close()

不幸的是,当我尝试设置该值时,我得到了 NullReferenceExeption.

Unfortunately, I get a NullReferenceExeption when I try to set the value.

我该怎么办?我已经导入了 Microsoft.Win32 并且 RegistryKey 不接受任何构造函数.

What should I do? I have imported Microsoft.Win32 and RegistryKey doesn't accept any constructor.

推荐答案

从键名中删除HKEY_LOCAL_MACHINE",这已经被代码中的 LocalMachine 成员覆盖了.

Remove "HKEY_LOCAL_MACHINE" from the key name, that's already covered by the LocalMachine member in your code.

请注意,此代码不适用于普通的 Vista 或 Win7 计算机,您无法在启用 UAC 的情况下打开此密钥进行写入.您需要一个需要管理员权限的清单.改为写入 My.Computer.Registry.CurrentUser.

Beware that this code will not work on a regular Vista or Win7 machine, you cannot open this key for writing with UAC enabled. You'd need a manifest that requires administrator privileges. Write to My.Computer.Registry.CurrentUser instead.

如果您在 64 位版本的 Windows 上运行它,则另一个复杂问题是注册表虚拟化.32 位程序将改为从 HKLM\Software\Wow6432Node 读取和写入 HKLM\Software 密钥.

One more complication is registry virtualization if you run this on the 64-bit version of Windows. 32-bit programs will read and write HKLM\Software keys to/from HKLM\Software\Wow6432Node instead.

这篇关于更改 VB.NET 中的注册表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆