10 Replies - 2802 Views - Last Post: 10 June 2012 - 04:30 AM Rate Topic: -----

#1 revno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 03-May 12

problem with searching key values in registry

Posted 04 June 2012 - 01:18 AM

Hello, i have problem with searching key value in registry
I have that
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim itm As ListViewItem
        Dim root As RegistryKey
        Dim searchkey As String = TextBox1.Text
        Dim matchtype As String = Nothing
        Dim str(5) As String
        For Each valueName As String In root.GetValueNames
            If valueName.Contains(searchkey) Then
                matchtype = "Value Name"
                str(0) = matchtype
                str(1) = root.Name
                str(2) = valueName
                str(3) = root.GetValue(valueName)
                itm = New ListViewItem(str)
                ListView1.BeginUpdate()
                ListView1.Items.Add(itm)
                ListView1.EndUpdate()
                Label11.text = Label11.Text = +1
            End If
        Next
    End Sub

But on line: For Each valueName As String In root.GetValueNames, it says: Object reference not set to an instance of an object.
please help

Is This A Good Question/Topic? 0
  • +

Replies To: problem with searching key values in registry

#2 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: problem with searching key values in registry

Posted 04 June 2012 - 05:06 AM

where do you assign a value to root?
Was This Post Helpful? 0
  • +
  • -

#3 revno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 03-May 12

Re: problem with searching key values in registry

Posted 04 June 2012 - 07:26 AM

How? please
Was This Post Helpful? 0
  • +
  • -

#4 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: problem with searching key values in registry

Posted 04 June 2012 - 09:48 AM

The registry is a sensitive area, so some times you can't access it without administrator credentials. Also the registry keys you need to access must exist. If you are typing a word (key name) on the textbox, you might need to include other information. Keys are not strings and also they could be subkeys of other keys.

In order to read it you may need to navigate to it or "open it", something like this:

        Dim root As RegistryKey
        root = Registry.LocalMachine.OpenSubKey("Software", True)



that gives you access to "HKEY_LOCAL_MACHINE\Software"

Check here:
http://omarabid.hubp...eys-with-VBnet.

Also you may have a mistake on line 18 of your code. If you want to display an amount(counter) you have to have integers there, maybe convert a value to integer, add to it and then convert to string again to display.
Was This Post Helpful? 0
  • +
  • -

#5 revno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 03-May 12

Re: problem with searching key values in registry

Posted 04 June 2012 - 10:29 AM

Not working... :(
Now when clicking on the button, nothing happens
Was This Post Helpful? 0
  • +
  • -

#6 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: problem with searching key values in registry

Posted 04 June 2012 - 02:00 PM

What do you enter on your textbox?

The code I gave you is just an example, not to be used just like that. You have several places where to look on the registry: Local Machine, Current User, Classes Root...

The registry is not a single file anymore.

Let me see if I can come with an example that works; in the mean time check where is the key you need to look for.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: problem with searching key values in registry

Posted 04 June 2012 - 02:07 PM

Also, if you search all the values in the root, you will probably find it very slow, since there are over a million values in the average registry. You'll want to narrow it down as ricardo suggests.

If you'd like to look in the registry to get an idea of how it's laid out, run regedit.exe. DON'T (repeat DON'T) change anything using this, unless you know exactly (repeat exactly) what you are doing.

This post has been edited by BobRodes: 04 June 2012 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: problem with searching key values in registry

Posted 05 June 2012 - 04:34 AM

I slightly modified the code on the discussion on this page:
http://bytes.com/top...ow-registry-key

To this:

Imports Microsoft.Win32

Public Class RegSearch

    Public Shared Sub Search(ByVal text As String)
        For Each key As RegistryKey In New RegistryKey() { _
        Registry.ClassesRoot, _
        Registry.CurrentConfig, _
        Registry.CurrentUser, _
        Registry.LocalMachine, _
        Registry.Users _
        }
            SearchKey(text, key)
        Next

        Console.WriteLine("Done")
    End Sub


    Private Shared Sub SearchKey(ByVal text As String, ByVal key As RegistryKey)

        If key Is Nothing Then
            Return
        End If

        If key.Name.IndexOf(text) > -1 Then
            MessageBox.Show("Found in " + key.ToString())
        End If

        For Each valueName As String In key.GetValueNames()
            If valueName.IndexOf(text) > -1 Then
                MessageBox.Show("Found in " + key.ToString() + ", " +
                valueName)
            End If

            Dim value As Object = key.GetValue(valueName)
            If Not value Is Nothing AndAlso _
            value.ToString().IndexOf(text) > -1 Then
                MessageBox.Show("Found in " + key.ToString() + ", " +
                valueName + ": " + value.ToString())
            End If
        Next

        For Each subKeyName As String In key.GetSubKeyNames()
            Try
                SearchKey(text, key.OpenSubKey(subKeyName))
            Catch ex As Security.SecurityException
                ' can't search this one, just skip it
            End Try
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Cursor = Cursors.WaitCursor
        Search(Trim(TextBox1.Text))
        Me.Cursor = Cursors.Default
    End Sub
End Class




Probably you could modify it and parse the returns to get your other strings to suit your needs.

I also tried this:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim readValue As String

        readValue = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFWARE\7-Zip", "Path", Nothing) ' Returned Empty
        MessageBox.Show("The value is " & readValue)

        readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Console", "ColorTable00", Nothing) 'Did Work Fine
        MessageBox.Show("The value is " & readValue)

        readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Control Panel\Cursors", "NWPen", Nothing) 'Did Work Fine

        MessageBox.Show("The value is " & readValue)

        readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Environment", "TEMP", Nothing) 'Did Work Fine

        MessageBox.Show("The value is " & readValue)

    End Sub


Was This Post Helpful? 0
  • +
  • -

#9 revno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 03-May 12

Re: problem with searching key values in registry

Posted 05 June 2012 - 04:35 AM

No, I want search specified registry value in textbox :)
Was This Post Helpful? 0
  • +
  • -

#10 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: problem with searching key values in registry

Posted 05 June 2012 - 05:25 AM

See line 55 above
Was This Post Helpful? 0
  • +
  • -

#11 revno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 03-May 12

Re: problem with searching key values in registry

Posted 10 June 2012 - 04:30 AM

ok, thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1