帮助阅读远程当前用户注册表项 [英] Help reading the remote current user registry keys

查看:91
本文介绍了帮助阅读远程当前用户注册表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究此功能,该功能将从远程计算机上的HKCU读取密钥. Microsoft声明您可以使用openremotebasekey进行此操作,但是我似乎无法从不稳定的环境区域获取一些信息.下面是我创建的函数,我的目标是从HKEY_CURRENT_USER \ Volatile Environment获取HOMEPATH值.如果有人可以告诉我我做错了什么,那我会不断收到以下错误消息.


错误:对象引用未设置为对象的实例.

I have been working on this function that will read keys from the HKCU on a remote computer. Microsoft states that you can do this with openremotebasekey but i cant seem to get it to rad some information from the voliatile environment area. below is the function i created and my goal is to get the HOMEPATH vaule from HKEY_CURRENT_USER\Volatile Environment. I keep getting the below error if someone can show me what im doing wrong that would be great.


Error: Object reference not set to an instance of an object.

Public Function ReadHKCUReg(ByVal strComputer As String, ByVal SubKey As String, ByVal KeyName As String) As String
    Dim str As String = DirectCast(Nothing, String)
    Dim CurrentUser As RegistryKey = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, strComputer)
    Dim Regvalue As RegistryKey = CurrentUser.OpenSubKey(SubKey)

    str = Regvalue.GetValue(KeyName)

    Return str

End Function

推荐答案

首先,我们不知道调用此函数的代码是什么样.

其次,在Dim CurrentUser As RegistryKey...行之后,您无需进行任何检查以查看您是否确实有对象.在下一行之后,您也不会这样做.

如果这些行之一返回Nothing,则下一行将失败,并显示您指定的错误.

最后,DirectCast(Nothing, String)是无用的.只需将其删除.当您声明一个字符串变量并且不分配任何内容时,它会自动初始化为Nothing.

也没有将Nothing转换为String的事情.最终结果是Nothing.
First, we have no idea what the code looks like that is calling this function.

Second, you''re not doing any checking after the Dim CurrentUser As RegistryKey... line to see if you actually have an object. Neither do you do it after the next line.

If either one of these lines returns Nothing, the next line will fail with the error you specified.

And finally, the DirectCast(Nothing, String) is useless. Just remove it. When you declare a string variable and don''t assign anything to it, it is automatically initialized to Nothing.

There is also no such thing as converting Nothing to String. The end result is Nothing.


这篇关于帮助阅读远程当前用户注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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