Programming the Registry using VB 6 and VB.NET
The good news is that all of the code above also works in VB.NET. Welllll ... It works after it's cleaned up to .NET standards.
Here's the .NET-ified version of the same program.
Public Class Form1
Dim ClickCounter As Integer
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
ClickCounter = 0
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseDown
ClickCounter = ClickCounter + 1
SaveSetting("AboutVBExample", _
"Location\Click" & ClickCounter, _
"button", e.Button)
SaveSetting("AboutVBExample", _
"Location\Click" & ClickCounter, _
"x", e.X)
SaveSetting("AboutVBExample", _
"Location\Click" & ClickCounter, _
"y", e.Y)
End Sub
End Class
Shift isn't a mouse property anymore so that had to be eliminated. And the mouse properties are passed as properties of the MouseEventArgs argument e. Other than that, it's pretty much the same. The code, that is.
The results are quite different. (This relates to .NET types rather than programming the registry. But differences like this might trip you up in a real situation so I thought I should cover it.) Compare this illustration with the one above.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Note that the button value is nothing like the one in VB 6. In VB.NET, this is a MouseButtons object with a value equal to "A bitwise combination of the MouseButtons enumeration values." Visual Studio Intellisense will confirm that MouseButtons.Right is 2097152. It's a lot more flexible, but like everything else in .NET, more complex.
The even better news is the host of new methods and properties that are also available in VB.NET. To introduce the Registry object, let's get the same value a different way using the GetValue method.
Since the whole registry is open with the .NET Registry object, our code has to do a few new things. First, the root nodes of the actual registry are all objects too. So we just use the root node that we're interested in (our data is in CurrentUser) as part of the object qualification of the Registry object like this:
Registry.CurrentUser ....
The names of the root instances are:
Next, we have to define a registry key in the code. Since we're still working with the old VB 6 node, we open a subkey passing using the complete key under one of the root instances as a parameter.
Software\VB and VBA Program Settings\AboutVBExample\Location\Click1
Finally, we can get the value of the node we're interested in. Here's the code that does it. (I had to insert editorial continuations to keep lines short. They're not in the actual code.)
Dim key As RegistryKey
Dim keyValue As String
Dim buttonVal As Integer
keyValue = _
"Software\VB and VBA ... <continued>
Program Settings\AboutVBExample\Location\Click4"
key = Registry.CurrentUser.OpenSubKey(keyValue, False)
buttonVal = CType(key.GetValue("button"), Integer)
key.Close()
We have to "cast" the value we retrieve as an Integer since it's always returned as as object.
Another way to accomplish the same thing is to use the GetValue method of the My.Computer.Registry object. The key statements here look like this:
buttonVal = CType(My.Computer.Registry.GetValue( _
"HKEY_CURRENT_USER\Software\ ... <continued>
VB and VBA Program Settings\ ... <continued>
AboutVBExample\Location\Click4", "button", 0), Integer)
Note that in this case, the path starts at the actual root, not the instance of the registry here. The default value of 0 isn't optional either. So there are some differences.
The Registry object gives you the ability to treat the registry pretty much like a database with methods like ...
... to name just a few.
For example, the code ...
Dim SubKeyNames As String()
SubKeyNames = key.GetSubKeyNames
... starting at the Location node will return the four keys Click1 through Click4.
This article has concentrated on processing just the registry entries that were added in the special VB 6 area to avoid taking any more risk than necessary when working with this critical resource. I advise you to do the same. Programming the registry isn't too dangerous, but changing keys and values that you don't understand is super dangerous.
Here's the .NET-ified version of the same program.
Public Class Form1
Dim ClickCounter As Integer
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
ClickCounter = 0
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseDown
ClickCounter = ClickCounter + 1
SaveSetting("AboutVBExample", _
"Location\Click" & ClickCounter, _
"button", e.Button)
SaveSetting("AboutVBExample", _
"Location\Click" & ClickCounter, _
"x", e.X)
SaveSetting("AboutVBExample", _
"Location\Click" & ClickCounter, _
"y", e.Y)
End Sub
End Class
Shift isn't a mouse property anymore so that had to be eliminated. And the mouse properties are passed as properties of the MouseEventArgs argument e. Other than that, it's pretty much the same. The code, that is.
The results are quite different. (This relates to .NET types rather than programming the registry. But differences like this might trip you up in a real situation so I thought I should cover it.) Compare this illustration with the one above.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Note that the button value is nothing like the one in VB 6. In VB.NET, this is a MouseButtons object with a value equal to "A bitwise combination of the MouseButtons enumeration values." Visual Studio Intellisense will confirm that MouseButtons.Right is 2097152. It's a lot more flexible, but like everything else in .NET, more complex.
The even better news is the host of new methods and properties that are also available in VB.NET. To introduce the Registry object, let's get the same value a different way using the GetValue method.
Since the whole registry is open with the .NET Registry object, our code has to do a few new things. First, the root nodes of the actual registry are all objects too. So we just use the root node that we're interested in (our data is in CurrentUser) as part of the object qualification of the Registry object like this:
Registry.CurrentUser ....
The names of the root instances are:
- CurrentUser
- LocalMachine
- ClassesRoot
- Users
- PerformanceData
- CurrentConfig
- DynData
Next, we have to define a registry key in the code. Since we're still working with the old VB 6 node, we open a subkey passing using the complete key under one of the root instances as a parameter.
Software\VB and VBA Program Settings\AboutVBExample\Location\Click1
Finally, we can get the value of the node we're interested in. Here's the code that does it. (I had to insert editorial continuations to keep lines short. They're not in the actual code.)
Dim key As RegistryKey
Dim keyValue As String
Dim buttonVal As Integer
keyValue = _
"Software\VB and VBA ... <continued>
Program Settings\AboutVBExample\Location\Click4"
key = Registry.CurrentUser.OpenSubKey(keyValue, False)
buttonVal = CType(key.GetValue("button"), Integer)
key.Close()
We have to "cast" the value we retrieve as an Integer since it's always returned as as object.
Another way to accomplish the same thing is to use the GetValue method of the My.Computer.Registry object. The key statements here look like this:
buttonVal = CType(My.Computer.Registry.GetValue( _
"HKEY_CURRENT_USER\Software\ ... <continued>
VB and VBA Program Settings\ ... <continued>
AboutVBExample\Location\Click4", "button", 0), Integer)
Note that in this case, the path starts at the actual root, not the instance of the registry here. The default value of 0 isn't optional either. So there are some differences.
The Registry object gives you the ability to treat the registry pretty much like a database with methods like ...
- GetSubKeyNames
- CreateSubKey and DeleteSubKey
- GetValueKind and GetValueNames
- SetValue
... to name just a few.
For example, the code ...
Dim SubKeyNames As String()
SubKeyNames = key.GetSubKeyNames
... starting at the Location node will return the four keys Click1 through Click4.
This article has concentrated on processing just the registry entries that were added in the special VB 6 area to avoid taking any more risk than necessary when working with this critical resource. I advise you to do the same. Programming the registry isn't too dangerous, but changing keys and values that you don't understand is super dangerous.
Source...