Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,763 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,148 people online right now. Registration is fast and FREE... Join Now!




Editing the Registry

 
Reply to this topicStart new topic

> Editing the Registry, Using code to make changes

Bort
Group Icon



post 10 Oct, 2008 - 05:40 AM
Post #1


Before we start anything here, BACK UP YOUR REGISTRY!!! Find out how here.

Now, if anything goes wrong and you inadvertently delete everything your computer needs to know, you can restore your registry to before we started.

This tutorial is effectively part 3 of my 'Trial Period' tutorial. The first part (here) covers how to set up an application with a 14-day trial period. The second part (here) deals with encrypting data so it cannot be tampered with. This, the third and final part , will show you how to add, delete, change, and read entries in the Windows Registry.

As with the other tutorials I've written, once I've gotten started, I've discovered that what I was trying to do was surprisingly easy. Even so, you do not want to break, or even slightly damage, the registry, hence the back up warning at the top.

The first thing we are going to look at is adding a new entry to the registry. For the purposes of this tutorial, we will use the Current User key in the registry (HKEY_CURRENT_USER). I suspect that when you actually add this to your own application, you will want to change to the Local Machine key where most of the software registry entries are. Regardless of where you put entries into the registry, you will need to know your way around there.

There are only 2 lines of code needed to add something into the registry.

CODE

        My.Computer.Registry.CurrentUser.CreateSubKey("VBTest")
        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\VBTest", "APPDATA", "Hello", Microsoft.Win32.RegistryValueKind.String)


NOTE:
If you have followed on to this tutorial from my encryption tutorial, replace the 2nd line of code above with the line below. this will allow you to add your encrypted data to the registry instead of a string which reads 'Hello'.

CODE

        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\VBTest", "APPDATA", bytHashedData, Microsoft.Win32.RegistryValueKind.Binary)


After running this piece of code (I recommend putting it into a Button_Click event), you can open the registry (Start > Run > regedit), expand the HKEY_CURRENT_USER tree, and listed underneath it, you will see a folder called VBTest. clicking on it, you will see (in the right hand pane) 2 entries. The first is (Default) which seems to appear every time something is added. Certainly every registry folder I checked had a default entry. The second will be the APPDATA entry we just added. Depending on which line of code you used, it will read either 'Hello' or will show a long string of numbers.

Next, we will look at deleting entries from the registry. Again, this can be accomplished by using only a single line of code, depending on what you want to do. There is a line to remove an entire subkey (we've been using VBTest as the subkey) or another to remove a specific value from the subkey (we've been using APPDATA). The lines of code are as follows.

CODE

        My.Computer.Registry.CurrentUser.DeleteValue("APPDATA")

        My.Computer.Registry.CurrentUser.DeleteSubKey("VBTest")


If you still have the registry window open, press F5 to refresh it after running either of these lines. When you check, either the VBTest folder or the APPDATA entry will have gone.

The next part of the tutorial will look at editing a registry entry. Say, for example, you had someone trialling a piece of your software and they then bought the full version from you, this is the code you would use to change the registry entry which held the trial information.

CODE

       Dim regstring = My.Computer.Registry.CurrentUser.OpenSubKey("VBTest", True)

        regstring.SetValue("APPDATA", "Goodbye", Microsoft.Win32.RegistryValueKind.String)


Note, you could also use this to change the type of information stored in the entry. The final part of the SetValue line (RegistryValueKind) allows you to specify the type of data in the entry. Delete String from the line above, and take a look at your options.

Finally, we will look at simply reading a registry entry. All this does is open the entry, read the value, and post it in a MessageBox for you.

CODE

        Dim readvalue As String

        readvalue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\" & key, "APPDATA", Nothing)
        MessageBox.Show("The value is " & readvalue)


Please note, however, that this is a little temperamental as to which data types it will display in a MessageBox. This is the same for any VB application though, and most of these you can get around by adding ".ToString" onto the end of the variable name.

Over the next week or so, I will be working on putting together my 3 recent tutorials, Trial Period, Encryption, and Registry Entries so they will work together. I will post them on /dic when they are finished, but until then, if you have any questions, please post them here.

Happy coding,
Bort
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/20/08 12:47PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month