• 0

[VB.NET] How do you get the program to read...


Question

Q: How do you get the program to read a Registry Key?

I know how to Create a Registry Key with:

My.Computer.Registry.CurrentUser.CreateSubKey("MyTestKey")

Thanks for any help! :D

Link to comment
Share on other sites

Recommended Posts

  • 0

I see you are developing with VB.Net 2005...

But remember, you can't distribute your applications with it, it's in beta!

In VB.Net 2003 it would be

Dim s as string = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("Microsoft").CreateSubKey("Windows").CreateSubKey("CurrentVersion").CreateSubKey("Run").GetValue("Myvalue")

This would read HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run and then the value of Myvalue.

I hope it helps

Link to comment
Share on other sites

  • 0

Does this work?

Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run").GetValue("Myvalue")

Link to comment
Share on other sites

  • 0

I know this code is wrong, so can you help me sort it out?

   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If New System.Windows.Forms.TextBox = ("45345454") Then
            Welcome.Show()
        Else
            Application.Exit()
        End If
    End Sub

Thanks again!

Link to comment
Share on other sites

  • 0
I know this code is wrong, so can you help me sort it out?

? ?Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 ? ? ? ?If New System.Windows.Forms.TextBox = ("45345454") Then
 ? ? ? ? ? ?Welcome.Show()
 ? ? ? ?Else
 ? ? ? ? ? ?Application.Exit()
 ? ? ? ?End If
 ? ?End Sub

Thanks again!

Ok how do I check a textbox to see if they entered a right code?


Thanks:Dnyone that can help me... :D

Link to comment
Share on other sites

  • 0
Ok how do I check a textbox to see if they entered a right code?

forget about that code... :blink:

Thanks anyone that can help me... :D

Ummm what exactly are you doing? :s

Link to comment
Share on other sites

  • 0
Ummm what exactly are you doing? :s

Don't worry about that now.

I get this error now:

"Overload resolution failed because no accessible 'SetValue' accepts this number of arguments."

When I try to used:

Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("LBluePlanet").CreateSubKey("CurrentVersion").SetValue("333")

Thanks for any help you can get me! :D

Edited by gerry.74
Link to comment
Share on other sites

  • 0

Well there's no Overloaded Method header for the SetValue method, basically you forgot to specify the object...

(string, object) was required, unless you want to use the other overloaded method header, try this now

Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("LBluePlanet").CreateSubKey("WiseOne").CreateSubKey("CurrentVersion").SetValue("333",new Integer)

Link to comment
Share on other sites

  • 0

Ok, how do you get it to find that Value?

This is what I got so far:

Code to Set the Value:

Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("LBluePlanet").CreateSubKey("CurrentVersion").SetValue("1", New Integer)

Code to Get the Value:

Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software").CreateSubKey("LBluePlanet").CreateSubKey("CurrentVersion").GetValue("1", New Integer)

Thanks again :D

Edited by gerry.74
Link to comment
Share on other sites

  • 0

ummmm you're right but, why use "1" ? you're trying to obtain the value back from the name of the key "333" so you use "333" again, and the other parameter required for the GetValue, is what's returned if that specific key specified isn't found.

Link to comment
Share on other sites

  • 0

So, I need to delete "New Integer" and enter a new parameter, because "New Integer" is saying somthing else, right?

Link to comment
Share on other sites

  • 0
So, I need to delete "New Integer" and enter a new parameter, because "New Integer" is saying somthing else, right?

No No... Basically for GetValue, the second part of the parameter that's required is any Object, that you want to be returned if the key isn't located.

Link to comment
Share on other sites

  • 0
I know this code is wrong, so can you help me sort it out?

? ?Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 ? ? ? ?If New System.Windows.Forms.TextBox = ("45345454") Then
 ? ? ? ? ? ?Welcome.Show()
 ? ? ? ?Else
 ? ? ? ? ? ?Application.Exit()
 ? ? ? ?End If
 ? ?End Sub

Thanks again!

Does that even compile? I'm not sure myself.

Your form should presumably have a TextBox control on it. You need to compare that textbox's "Text" property with that "43..." string. Your code is attempting to create a new TextBox control every time it's run, which won't work at all. Assuming, of course, it even compiles in the first place...

Let's assume your text box is called txtNumber (i.e. its Name property):

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  If (txtNumber.Text = "45345454") Then
    Welcome.Show()
  Else
    Application.Exit()
  End If
End Sub

Link to comment
Share on other sites

  • 0
No No... Basically for GetValue, the second part of the parameter that's required is any Object, that you want to be returned if the key isn't located.

It still won't work.... :no:

I have tried everything I know.

Anyone?

Link to comment
Share on other sites

  • 0

I think you're getting confused between registry keys and registry values, which isn't helping.

In the registry, there are keys and there are values. If the registry was an file system like NTFS, registry keys would be directories and registry values would be files. Registry values, like files, contain data - be it numeric, string or binary.

Fire up RegEdit for a moment. You'll see its window is split into two panes. The left one contains keys. The right one contains values.

Registry keys can be nested - i.e. keys can contain other keys, like directories can contain other directories.

For example:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer is a registry key.

This key contains a number of values. Each value has a name and a piece of data that the value holds.

For example:

Within the "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer" key on my system, I can see a value with the name "Logon User Name", whose data is set to "Zoidberg".

Registry keys cannot contain data directly. To store data within registry keys, you must first create values. The values hold the data.

Let's say you want to store some data in the registry. First, you need to make sure the key exists. Then you create the values, assigning the data to them.

Example code (I'm a C# programmer myself, but this should be pretty much correct (I hope)):

Imports System
Imports Microsoft.Win32

Public Sub SetTheValue
  Dim r As RegistryKey
  r = Registry.CurrentUser.CreateSubKey("Software\LBluePlanet\"CurrentVersion")
  
  r.SetValue("MyValue", 1)
  r.Close()
End Sub

Public Function GetTheValue() As Integer
  Dim result As Integer

  Dim r as RegistryKey
  r = Registry.CurrentUser.OpenSubKey("Software\LBluePlanet\"CurrentVersion")
  result = CType(r.GetValue("MyValue", 1), Integer)

  r.Close()

  Return result
End Function

So, what does this do?

The first method, SetTheValue, creates a registry key in HKEY_CURRENT_USER called "Software\LBluePlanet\"CurrentVersion". It then adds a value to this key, with the name "MyValue". The data associated with the "MyValue" value within the registry key is the numeric value 1. Finally, the registry key is closed and the method exits.

The second part, the GetTheValue function, reads in the value from the registry and returns it to you as an integer. It opens the "Software\LBluePlanet\"CurrentVersion" registry key. Next, it obtains the data associated with the "MyValue" value and casts it to an integer. The "1" in the arguments to r.GetValue() provides a default value in case the value doesn't exist. The registry key is closed and the data associated with the value is returned. The end.

You should also note that each registry key has a default value. If you want to set or get the data associated with this, you just pass a blank string as the first parameter to the SetValue() or GetValue() methods of the RegistryKey class.

All of this stuff is in the .NET framework help and therefore accessible to all.

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfmicrosoftwin32registrykeyclasstopic.htm

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfmicrosoftwin32registrykeyclasscreatesubkeytopic.htm

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfmicrosoftwin32registrykeyclassgetvaluetopic.htm

ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfmicrosoftwin32registrykeyclasssetvaluetopic.htm

Link to comment
Share on other sites

  • 0

:blink: I look at this code and I found nothing wrong but VB .net see it different.

It says this code:

result = CType(r.GetValue("Welcome", 1), Integer)

Object reference not set to an instance of an object.

Thanks again.

Link to comment
Share on other sites

  • 0
:blink: I look at this code and I found nothing wrong but VB .net see it different.

It says this code:

result = CType(r.GetValue("Welcome", 1), Integer)

Thanks again.

Anyone?? :cry: Please?

Link to comment
Share on other sites

  • 0

Ok, I have try, I try with different codes and everthing but I can't work it out, can anyone help me?

This error comes up when I debug my program

Object reference not set to an instance of an object.

and its highlights this code:

result = CType(r.GetValue("Welcome", 1), Integer)

Thanks again, and sorry for all the post.. :shifty:

Link to comment
Share on other sites

  • 0

I try that but it comes up with this error for the "New":

Keyword does not name a type.

Heres the whole code:

       Public Function GetTheValue() As Integer
            Dim result As Integer

            Dim r As Microsoft.Win32.RegistryKey
            r = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\LBluePlanet\CurrentVersion")
            result = CType(r.GetValue("Welcome", 1), new Integer)

            r.Close()

            Return result
        End Function

Thanks again :D

Link to comment
Share on other sites

  • 0
I try that but it comes up with this error for the "New":

Heres the whole code:

? ? ? ?Public Function GetTheValue() As Integer
 ? ? ? ? ? ?Dim result As Integer

 ? ? ? ? ? ?Dim r As Microsoft.Win32.RegistryKey
 ? ? ? ? ? ?r = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\LBluePlanet\CurrentVersion")
 ? ? ? ? ? ?result = CType(r.GetValue("Welcome", 1), new Integer)

 ? ? ? ? ? ?r.Close()

 ? ? ? ? ? ?Return result
 ? ? ? ?En:DFunction

Thanks again :D

Oh oops now u got the code u dont put New sorry... :sll the function works, what's wrong? :s

Link to comment
Share on other sites

  • 0

This error comes up when I debug my program

Object reference not set to an instance of an object.

and its highlights this code:

result = CType(r.GetValue("Welcome", 1), Integer)

Thanks

Link to comment
Share on other sites

  • 0
This error comes up when I debug my program

and its highlights this code:

result = CType(r.GetValue("Welcome", 1), Integer)

Thanks

Show me the block of code where you invoke this function.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.