从注册表项获取值

时间:2018-07-27 17:27:29

标签: vb.net registry

当我尝试从注册表中获取一个值并显示在DataGridView上时,该值与注册表中存在的值不同。

这发生在DisplayVersion上,有时也发生在DisplayName上。

注册表中的值

enter image description here

我的应用中的值

enter image description here


我的代码是:

Dim _regKeysFind() As String = {"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
                                "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"}
    Try
        _dTableApps = New DataTable()
        With _dTableApps
            .Columns.Add("Aplicacao")
            .Columns.Add("Versao")
            .Columns.Add("Fabricante")
            .Columns.Add("x64")
        End With
        For Each _regKeyString As String In _regKeysFind
            Using _regKey As RegistryKey = Registry.LocalMachine.OpenSubKey(_regKeyString)
                For Each _subRegKey As String In _regKey.GetSubKeyNames
                    Try
                        'Get sub keys
                        With _regKey.OpenSubKey(_subRegKey)
                            Dim _name = "", _version As String = "",
                            _installLocation = "", _publisher = "",
                             _systemComponent = "", _releaseType = ""
                            Try
                                Dim _text As New TextBox
                                _text.Text = .GetValue("DisplayName").ToString
                                _name = _text.Text
                                _text = Nothing
                            Catch ex As Exception
                            End Try
                            Try                                    
                                _version &= .GetValue("DisplayVersion").ToString
                            Catch ex As Exception
                            End Try
                            Try
                                _installLocation = .GetValue("InstallLocation").ToString
                            Catch ex As Exception
                            End Try
                            Try
                                _publisher = .GetValue("Publisher").ToString
                            Catch ex As Exception
                            End Try
                            Try
                                _systemComponent = .GetValue("SystemComponent").ToString
                            Catch ex As Exception
                            End Try
                            Try
                                _releaseType = .GetValue("ReleaseType").ToString.ToLower
                            Catch ex As Exception
                            End Try
                            If (_name.ToString <> "" And _systemComponent <> "1" And _releaseType <> "update") Then _dTableApps.Rows.Add({_name.Trim, _version, _publisher.Trim, If(_regKeyString.Contains("6432"), "32", "64") & " bits"})
                            _name = Nothing
                            _installLocation = Nothing
                            _publisher = Nothing
                            _version = Nothing
                            _systemComponent = Nothing
                            _releaseType = Nothing
                        End With
                    Catch ex As Exception
                    End Try
                Next
            End Using
        Next
        With gridAplicacoes
            Try
                _dTableApps = _dTableApps.Select("", "Aplicacao").CopyToDataTable()
            Catch ex As Exception
            End Try
            .DataSource = _dTableApps
        End With           
    Catch ex As Exception
    End Try
    _regKeysFind = Nothing`

我该怎么做才能绕开它?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

以这种方式尝试。
我在列表中添加了SubKeysSubKey本身的空检查,因此您可以验证在解析列表时实际查看的Registry Ke。 />

我没有发现两个参考文献之间有任何差异。
另外,请注意,您在这些图片中显示的Registry Keys与同一键无关。
使用SubKey向导找到正确的向导。

Dim RegKeysFind() As String = {"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
                               "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"}
Dim dTableApps As DataTable = New DataTable()
With dTableApps
    .Columns.Add("Aplicacao", GetType(String))
    .Columns.Add("Versao", GetType(String))
    .Columns.Add("Fabricante", GetType(String))
    .Columns.Add("x64", GetType(String))
    .Columns.Add("RegKey", GetType(String))
End With

For Each RegKeyString As String In RegKeysFind
    Try
        Using RegKey As RegistryKey = Registry.LocalMachine.OpenSubKey(RegKeyString, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey)
            For Each SubRegKey As String In RegKey.GetSubKeyNames
                If SubRegKey IsNot Nothing Then
                    Dim DisplayName, DisplayVersion, InstallLocation, Publisher, SystemComponent, ReleaseType As String
                    With RegKey.OpenSubKey(SubRegKey)
                        DisplayName = If(.GetValue("DisplayName") IsNot Nothing, .GetValue("DisplayName").ToString, "")
                        DisplayVersion = If(.GetValue("DisplayVersion") IsNot Nothing, .GetValue("DisplayVersion").ToString, "")
                        If DisplayVersion IsNot Nothing Then
                            Dim FirstWhiteChar As Integer = DisplayVersion.IndexOf(" "c)
                            If FirstWhiteChar > -1 Then
                                DisplayVersion = DisplayVersion.Substring(0, FirstWhiteChar)
                            End If
                        End If
                        InstallLocation = If(.GetValue("InstallLocation") IsNot Nothing, .GetValue("InstallLocation").ToString, "")
                        Publisher = If(.GetValue("Publisher") IsNot Nothing, .GetValue("Publisher").ToString, "")
                        SystemComponent = If(.GetValue("SystemComponent") IsNot Nothing, .GetValue("SystemComponent").ToString, "")
                        ReleaseType = If(.GetValue("ReleaseType") IsNot Nothing, .GetValue("ReleaseType").ToString, "")
                        If (Not String.IsNullOrEmpty(DisplayName) AndAlso (Not String.IsNullOrEmpty(SystemComponent) AndAlso (ReleaseType <> "update"))) Then
                            dTableApps.Rows.Add({DisplayName, DisplayVersion, Publisher, If(RegKeyString.Contains("WOW6432Node"), "32", "64") & " bits", SubRegKey})
                        End If
                    End With
                End If
            Next
        End Using
    Catch ex As Exception
        Console.WriteLine(ex.Message)
        Throw
    End Try
Next

dTableApps.DefaultView.Sort = dTableApps.Columns(0).ColumnName & " Asc"
gridAplicacoes.DataSource = dTableApps

答案 1 :(得分:0)

我使用代码解决了问题:

 Dim _regKeysFind() As String = {"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
        "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"}
    Try
        _dTableApps = New DataTable()
        With _dTableApps
            .Columns.Add("Aplicacao", GetType(String))
            .Columns.Add("Versao", GetType(String))
            .Columns.Add("Fabricante", GetType(String))
            .Columns.Add("x64", GetType(String))
            '.Columns.Add("RegKey", GetType(String))
        End With
        For Each _regKeyString As String In _regKeysFind
            Using _regKey As RegistryKey = Registry.LocalMachine.OpenSubKey(_regKeyString)
                For Each _subRegKey As String In _regKey.GetSubKeyNames
                    If (_subRegKey IsNot Nothing) Then
                        'Get sub keys
                        With _regKey.OpenSubKey(_subRegKey)
                            Dim _name As String = "", _version As String = "", _installLocation As String = "",
                                _publisher As String = "", _systemComponent As String = "", _releaseType As String = ""
                            Try
                                _name = .GetValue("DisplayName").ToString
                                _name = _name.Split(Chr(0))(0)
                            Catch ex As Exception
                            End Try
                            Try
                                _version &= .GetValue("DisplayVersion").ToString
                                _version = _version.Split(Chr(0))(0)
                            Catch ex As Exception
                            End Try
                            Try
                                _installLocation = .GetValue("InstallLocation").ToString
                            Catch ex As Exception
                            End Try
                            Try
                                _publisher = .GetValue("Publisher").ToString
                                _publisher = _publisher.Split(Chr(0))(0)
                            Catch ex As Exception
                            End Try
                            Try
                                _systemComponent = .GetValue("SystemComponent").ToString
                            Catch ex As Exception
                            End Try
                            Try
                                _releaseType = .GetValue("ReleaseType").ToString.ToLower
                            Catch ex As Exception
                            End Try
                            If (Not String.IsNullOrEmpty(_name) AndAlso _systemComponent <> "1" AndAlso _releaseType <> "update") Then _dTableApps.Rows.Add({_name, _version, _publisher, If(_regKeyString.Contains("6432"), "32", "64") & " bits"})
                            _name = Nothing
                            _installLocation = Nothing
                            _publisher = Nothing
                            _version = Nothing
                            _systemComponent = Nothing
                            _releaseType = Nothing
                        End With
                    End If
                Next
            End Using
        Next
        With gridAplicacoes
            _dTableApps.DefaultView.Sort = _dTableApps.Columns(0).ColumnName & " Asc"
            .DataSource = _dTableApps
        End With
    Catch ex As Exception
        'MsgBox(ex.Message)
    End Try
    _regKeysFind = Nothing