如何在VB NET中读取远程注册表? [英] How do I Read Remote Registry in VB NET ?

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

问题描述

您好,我正在尝试获取许多远程PC的架构和操作系统。为了做到这一点,我正在查询Win32_OperatingSystem并解析O.S的Caption。以及用于阅读OSArchitecture的架构。在Windows XP中,这个值不存在,所以我认为读取HKLM \SYSTEM®\\ CurrentControlSet \Control \ Session Manager \ Environment \ SPROCESSOR_ARCHITECTURE就可以完成这个代码的技巧:

 尝试 

Dim co < span class =code-keyword> As ConnectionOptions
co.Impersonation = ImpersonationLevel.Impersonate
co.Authentication = AuthenticationLevel .PacketPrivacy
co.EnablePrivileges = True

co.Username = username
co.Password = password

Dim 范围作为 ManagementScope( \\& machine.Text& \root \cimv2,co)

scope.Connect()

Dim environmentKey,asd2 As Microsoft.Win32.RegistryKey
Dim asd As String

environmentKey = RegistryKey.OpenRemoteBaseKey(RegistryHive .LocalMachine,machine.Text)
asd2 = environmentKey.OpenSubKey( SYSTEM \ CurrentControlSet \ Control \ \\ _Session Manager \ Environment True
asd = asd2.GetValue( PROCESSOR_ARCHITECTURE

Debug.Print( asd: + asd)

environmentKey.Close()

Catch ex As Exception

MessageBox.Show(ex.ToString)

< span class =code-keyword>结束
尝试





我的问题是:如果我尝试这个代码,我得到一个System.Security.SecurityException:不允许访问远程注册表



我是,我知道管理员用户名和密码。事实上,如果我运行一个简单的cmdkey / add:targetname / user:username / pass:password它可以工作。



那我为什么要运行cmdkey / add即使我已经在ConnectionOptions中指定了用户名和密码?



PS抱歉,我的英文不好

解决方案

事情是你没有为注册表访问指定用户名和密码。



您尝试使用ManagementScope类,它只适用于WMI,而不适用于Registry类。



此外,如果您的代码运行在32位机器,你将有访问64位注册表的问题。



所以,不要使用注册表来执行此操作。请改用WMI。在WMI中有两种方法可以做到这一点。您可以在WMI中使用标准注册表提供程序,也可以查询 Win32_OperatingSystem [ ^ ]类来获取你正在寻找的信息。



Win32_OperatingSystem查询方法比注册表方法更好,因为你不会遇到问题从32位计算机访问64位注册表。


好的,谢谢Dave Kreskowiak和Google,我明白了:

  Const  HKEY_current_user 作为 字符串 =   80000002 

Dim 选项作为 ConnectionO ptions
options.Impersonation = ImpersonationLevel.Impersonate
options.EnablePrivileges = True
options.Username = 。\ administrator
options.Password = my_password

Dim myScope 正如 ManagementScope( \\\ \\& RemotePCHostname& \root \default,options)
Dim mypath 作为 ManagementPath( StdRegProv
Dim mc 作为 ManagementClass(myScope,mypath, Nothing

Dim inParams As ManagementBaseObject = mc.GetMethodParameters( GetDWORDValue
inParams( hDefKey)= UInt32 .Parse(HKEY_current_user,System.Globalization.NumberStyles.HexNumber)' RegistryHive.LocalMachine
inParams( sSubKeyName) = SYSTEM \ CurrentControlSet \Control\Session Manager \ Environment
inParams ( sValueName)= < span class =code-string> PROCESSOR_ARCHITECTURE

Dim outParams As ManagementBaseObject = mc.InvokeMethod( GetStringValue,inParams, Nothing

如果(outParams( ReturnValue)。ToString()= 0 然后
MessageBox.Show(outParams( sValue)。ToString ())
Else
MessageBox.Show( 检索值时出错: + outParams( ReturnValue)。 ToString())
结束 如果


Hello, I'm trying to get the architecture and the operating system of many remote pcs. In order to do that i'm querying Win32_OperatingSystem and parsing the "Caption" for the O.S. and for the architecture im reading OSArchitecture . In Windows XP this value does not exists, so i thought that reading the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE would have done the trick like this code:

    Try

    Dim co As New ConnectionOptions
    co.Impersonation = ImpersonationLevel.Impersonate
    co.Authentication = AuthenticationLevel.PacketPrivacy
    co.EnablePrivileges = True

    co.Username = username
    co.Password = password

    Dim scope As New ManagementScope("\\" & machine.Text & "\root\cimv2", co)

    scope.Connect()

    Dim environmentKey, asd2 As Microsoft.Win32.RegistryKey
    Dim asd As String

    environmentKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine.Text)
    asd2 = environmentKey.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", True)
    asd = asd2.GetValue("PROCESSOR_ARCHITECTURE")

    Debug.Print("asd: " + asd)

    environmentKey.Close()

Catch ex As Exception

    MessageBox.Show(ex.ToString)

End Try



My problem is: if im trying this code I get an System.Security.SecurityException: "Accessing remote registry not permitted"

I am, and i know the administrator username and password. In fact if I run a simple cmdkey /add:targetname /user:username /pass:password It works.

So why do I have to run a cmdkey /add even if i have alredy specified the username and password in the ConnectionOptions ??

P.S. Sorry for my bad English

解决方案

The thing is you did NOT specify a username and password for the registry access.

You tried using the ManagementScope class, which only works with WMI, not the Registry classes.

Also, if your code is running on a 32-bit machine, you're going to have problems with accessing a 64-bit registry.

So, don't use the Registry to do this. Use WMI instead. There are two ways to do this in WMI. You can either use the Standard Registry Provider in WMI or you can query the Win32_OperatingSystem[^] class to get the information you're looking for.

The Win32_OperatingSystem query method is better than the registry method as you won't run into issues with accessing a 64-bit registry from a 32-bit machine.


All right, thank to Dave Kreskowiak and Google, i got it:

Const HKEY_current_user As String = "80000002"

Dim options As New ConnectionOptions
options.Impersonation = ImpersonationLevel.Impersonate
options.EnablePrivileges = True
options.Username = ".\administrator"
options.Password = "my_password"

Dim myScope As New ManagementScope("\\" & RemotePCHostname & "\root\default", options)
Dim mypath As New ManagementPath("StdRegProv")
Dim mc As New ManagementClass(myScope, mypath, Nothing)

Dim inParams As ManagementBaseObject = mc.GetMethodParameters("GetDWORDValue")
inParams("hDefKey") =  UInt32.Parse(HKEY_current_user,System.Globalization.NumberStyles.HexNumber) 'RegistryHive.LocalMachine
inParams("sSubKeyName") = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
inParams("sValueName") = "PROCESSOR_ARCHITECTURE"

Dim outParams As ManagementBaseObject = mc.InvokeMethod("GetStringValue", inParams, Nothing)

If (outParams("ReturnValue").ToString() = "0") Then
    MessageBox.Show(outParams("sValue").ToString())
Else
    MessageBox.Show("Error retrieving value : " + outParams("ReturnValue").ToString())
End If


这篇关于如何在VB NET中读取远程注册表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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