如何查找注册表项? 32/64位

时间:2014-02-24 10:47:14

标签: vb.net visual-studio registry visual-studio-2013 32bit-64bit

如何查找注册表项?我需要从32位和64位系统中查看3个不同的GUID。我需要将发现InstallLocation的那个返回给TextBox。我做了以下代码。我不知道它是否真的可用。我是初学者。请帮忙。

Imports Microsoft.Win32
Imports System.Net
Imports System

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Is64Bit As Boolean

        Is64Bit = String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))

        If Not Is64Bit Then
            Try
                Dim rk32_1 As RegistryKey
                rk32_1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812EU}_is1")
                Dim il_rk32_1 As String = rk32_1.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk32_2 As RegistryKey
                rk32_2 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812NA}_is1")
                Dim il_rk32_2 As String = rk32_2.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk32_3 As RegistryKey
                rk32_3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812RU}_is1")
                Dim il_rk32_3 As String = rk32_3.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
        End If

        If Is64Bit Then
            Try
                Dim rk64_1 As RegistryKey
                rk64_1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812EU}_is1")
                Dim il_rk64_1 As String = rk64_1.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk64_2 As RegistryKey
                rk64_2 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812NA}_is1")
                Dim il_rk64_2 As String = rk64_2.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk64_3 As RegistryKey
                rk64_3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812RU}_is1")
                Dim il_rk64_3 As String = rk64_3.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
        End If

    End Sub

End Class

3 个答案:

答案 0 :(得分:1)

我没有过多看你的代码,因为你做了相反的检查。 Wow64指的是64位机器上的32位应用程序。

来自Wikipedia

  

WoW64(Windows 64位Windows 32位)

     

WoW64子系统还处理运行32位的其他关键方面   应用。它涉及管理32位的交互   应用程序与Windows组件,如注册表,其中   具有用于64位和32位应用程序的不同密钥。例如   HKEY_LOCAL_MACHINE \ Software \ Wow6432Node是32位的等价物   HKEY_LOCAL_MACHINE \ Software(虽然32位应用程序不是   意识到这种重定向)。某些注册表项是从64位映射的   他们的32位等价物,而其他人有他们的内容   镜像,取决于Windows的版本。

PS:只是要注意它,System32SysWOW64 windows目录也会发生同样的事情。

  

更新:

只是一种简单的方法来检索操作系统是否是x64(而不是获取可能在x86操作系统中伪造的环境变量PROCESSOR_ARCHITEW6432):

' Get OS Architecture
' ( By Elektro)
'
' Usage Examples :
' Dim OSArchitecture As Architecture = GetOSArchitecture()
' MsgBox(OSArchitecture.ToString)
'
''' <summary>
''' Determines whether the OS is 32 or 64 Bits.
''' </summary>
''' <returns>
''' The return value could be: 
''' '32' for 32-Bit OS (x86)
''' '64' for 64-Bit OS (x64)
''' </returns>
Private Function GetOSArchitecture() As Architecture

    Return [Enum].Parse(GetType(Architecture),
                        Runtime.InteropServices.Marshal.SizeOf(GetType(IntPtr)) * 8)

End Function

''' <summary>
''' Indicates the possible processor architectures.
''' </summary>
Private Enum Architecture As Integer

    ''' <summary>
    ''' 32-Bit
    ''' </summary>
    x86 = 32

    ''' <summary>
    ''' 64-Bit
    ''' </summary>
    x64 = 64

End Enum

答案 1 :(得分:0)

首先,请记住,如果程序编译为x86,(IntPtr.Size * 8)将始终返回32(如果这是一个问题,我确实有一个类可以获得操作系统版本,无论如何如何编译exe)

我建议使用以下内容:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    ' Check if the OS is 64 bit
    Dim Is64Bit As Boolean = ((IntPtr.Size * 8) = 64)

    Dim UninstallPath As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

    If Is64Bit Then
        UninstallPath = UninstallPath.Insert(8, "\Wow6432Node")
    End If

    Dim il_rk32_1 As String = Nothing
    Dim il_rk32_2 As String = Nothing
    Dim il_rk32_3 As String = Nothing

    Try
        Dim rk32_1 As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallPath & "{1EAC1D02-C6AC-4FA6-9A44-96258C37C812EU}_is1")

        If rk32_1 IsNot Nothing Then
            Dim Val As Object = rk32_1.GetValue("InstallLocation")

            If Val IsNot Nothing Then
                il_rk32_1 = Val.ToString
            End If
        End If

    Catch ex As Exception

    End Try

    If String.IsNullOrEmpty(il_rk32_1) Then
        il_rk32_1 = "C:\Games\World_of_Tanks\"
    End If

    Me.TextBox1.Text = il_rk32_1


    Try
        Dim rk32_2 As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallPath & "{1EAC1D02-C6AC-4FA6-9A44-96258C37C812NA}_is1")

        If rk32_2 IsNot Nothing Then
            Dim Val As Object = rk32_2.GetValue("InstallLocation")

            If Val IsNot Nothing Then
                il_rk32_2 = Val.ToString
            End If
        End If

    Catch ex As Exception

    End Try

    If String.IsNullOrEmpty(il_rk32_2) Then
        il_rk32_2 = "C:\Games\World_of_Tanks\"
    End If

    Me.TextBox2.Text = il_rk32_2


    Try
        Dim rk32_3 As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallPath & "{1EAC1D02-C6AC-4FA6-9A44-96258C37C812RU}_is1")

        If rk32_3 IsNot Nothing Then
            Dim Val As Object = rk32_3.GetValue("InstallLocation")

            If Val IsNot Nothing Then
                il_rk32_3 = Val.ToString
            End If
        End If

    Catch ex As Exception

    End Try

    If String.IsNullOrEmpty(il_rk32_3) Then
        il_rk32_3 = "C:\Games\World_of_Tanks\"
    End If

    Me.TextBox3.Text = il_rk32_3

End Sub

编辑:

我已经改变了上面的代码以适应OP的问题(在评论中)

答案 2 :(得分:0)

为了访问从.reg文件安装的应用程序配置,我必须编写以下函数:

Public Const RegRoot As String = "Software\MyApp\"

Function ReadFromRegister(ByRef FieldToRead As String) As String
    ReadFromRegister = ""
    Dim MyReg As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    If MyReg Is Nothing Then
        End
    End If

    Dim MySubReg As RegistryKey = MyReg.OpenSubKey(RegRoot)
    If MySubReg Is Nothing Then
        End
    End If

    ReadFromRegister = MySubReg.GetValue(FieldToRead)
    MySubReg.Close()
    MyReg.Close()

End Function

请注意,函数OpenBaseKey是从 Framework 4 引入的。