vb.net 2010 - 从注册表读取不起作用 - win 7 [英] vb.net 2010 - reading from registry doesn't work - win 7

查看:56
本文介绍了vb.net 2010 - 从注册表读取不起作用 - win 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这会很简单....

I thought this would be dead simple however....

是的,所以我只是想从我的注册表中读取一个值.我已经经历了几个例子,但无法让它们中的任何一个工作.我也试过在管理员模式下运行我的应用程序,但仍然没有.有人可以帮忙吗?

Right, so all I'm simply trying to do is read a value from my registry. I have been through several examples but can't get any of them to work. I've also tried running my application in Admin mode and still nothing. Can someone please help?

从我尝试过的所有示例中,我将使用最简单的一个.

From all my examples that I've tried, I'll use the simplest one.

这有效:

Dim val As String
val = Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString()
MsgBox(val)

这个(我想要的)没有:

This (the one I want) doesn't:

Dim val As String
val = Registry.LocalMachine.OpenSubKey("SOFTWARE\PTSClient").GetValue("ConfigDB").ToString()
MsgBox(val)

后一个路径和值是我在注册表中手动创建的.我检查了两者之间的权限,它们完全相同.我也试过以管理员身份运行该应用程序.我在 val= 行上收到一个运行时错误,它说:使用new"关键字创建对象实例.

THe latter path and value is one that I've manually created in the registry. I've checked the permissions between the two and they are the exact same. I've also tried running the app as administrator. I get a runtime error on the val= line, it says: Use the "new" keyword to create an object instance.

有什么想法吗?所有各种在线示例都失败了,对于我的一生,我无法弄清楚为什么......

Any ideas? All the various online examples have failed and for the life of me, I can't figure out why...

干杯,J

推荐答案

好吧,我已经使用为 x86 编译的示例应用程序尝试了您的代码,但正如预期的那样,它失败并出现空值异常.

Well, I have tried your code with a sample application compiled for x86 and, as expected, it fails with a null value exception.

我假设您正在为 x86 模式构建应用程序并在 64 位环境中运行.
当然,如果不是这样,请告诉我,我会删除此答案.

I assume you are building an application for x86 mode and running in a 64bit environment.
Of course, if this is not the case, let me know and I will delete this answer.

在上述情况下,对 LocalMachine.Software 注册表路径中的读/写调用将被操作系统自动更改为在 Software\Wow6432Node 子项中读/写,因此,您的代码无法找到您的手动插入的密钥(SOFTWARE\PTSClient").
此代码将从 Registry.LocalMachine.OpenSubKey("SOFTWARE\PTSClient") 返回一个空值,导致无法获取 ConfigDB 值.

In the situation outlined above, the calls to read/write in the LocalMachine.Software registry path will be automatically changed by the Operating System to read/write in the Software\Wow6432Node subkey and thus, your code is unable to find your manually inserted key ("SOFTWARE\PTSClient").
This code will give a null value as return from Registry.LocalMachine.OpenSubKey("SOFTWARE\PTSClient") leading to the failure to get the ConfigDB value.

您需要将您的键/值添加到 Software\Wow6432Node 路径或为 AnyCPU 模式编译您的应用程序或让您的代码将值写入寄存器(它将被重定向到 Wow6432Node).

You need to add your keys/values to the Software\Wow6432Node path or compile your application for AnyCPU mode or let your code write the value to the register (it will be redirected to the Wow6432Node).

这篇关于vb.net 2010 - 从注册表读取不起作用 - win 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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