Tutorial 6: How to use (add, read, change, delete) registry keys with vb.net
Find out more tutorials at http://thedotnetsource.blogspot.com/
Reason of this project:
This is the next part of the previous tutorial. We'll learn here how to add, change, read, delete registry keys and values using vb.net.
I included a sample that contains all those functionality.
Project details:
1- How to add a registry key/value
2- How to read a registry value
3- How to delete a key or a value
4- Changing a value or a key
5- Hints to use registry with VB.net
6- The registry reader (VB.net)
1- How to add a registry key/value
One thing that I think I forget to notice. A folder in the registry is name "key" and the elements in this key are named "values".
There's a description of each value type in the 5th tutorial
Now we'll see how to add a key or a value.
Probably you have thought where we'll put it and whether it's a key or a value.
So we have 2 things to notice.
Visual Basic will automatically show you the hives (they are stored in).
You'll have only to choose the needed one.
To do that paste the following line.
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
This line will create a key in the HKEY_CURRENT_USER hive and will be named "testkey"
Now let's move on to see how to set a value.
For the value we'll need three things: Value path, Value name and value value. We can also precise the value type if not Visual Basic will try to assign the value type depending on the object type.
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "TestValue", "This is a test value.")
Tip: Type "," and Visual Basic will show a list of value types.
The sample:
The sample contain two part, one for creating keys and the other to create values.
To create a key just put the named of the key and it'll be created in the Current_User folder.
To assign a value, type the complete path, for example "HKEY_CURRENT_USER\mykey" and then the value name then the value content and click add to create it.
2- How to read a registry value
The next thing is to read what we wrote!
This is so simple, just put the following line.
You'll need to have the Path and the name of the value.
Dim readValue As String
readValue = My.Computer.Registry.GetValue _
("HKEY_CURRENT_USER\TestKey", "TestValue", Nothing)
MsgBox("The value is " & readValue)
If you wish to do more complex things, like getting the keys in a hive and getting the values of keys...
Then you should see the registry viewer sample. (Download the source code to get it)
3- How to delete a registry key or value
We finish by deleting what we added.
To do this it's easy!
The following line will delete a key
My.Computer.Registry.CurrentUser.DeleteSubKey("TestKey")
To delete a value
my.Computer .Registry .CurrentUser.DeleteValue("Test value")
4- Changing a value or a key
Changing a value can be somewhat difficult.
Dim autoshell = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
'' Set the value to 0
autoshell.SetValue("autorestartshell", 0)
autoshell.Close()
After opening a subkey (don't forget true! as it give you permission to delete), we can use the autoshell in order to change any value in the following subkey.
5- Hints to use registry with VB.net
We can count the values in a hive
My.Computer.Registry.CurrentUser.ValueCount.ToString()
But also the keys
My.Computer.Registry.CurrentUser.SubKeyCount.ToString()
And check if a value exist
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\MyKey", _
"TestValue", Nothing) Is Nothing Then
MsgBox("Value does not exist.")
Else
MsgBox("Value exist.")
End If
6- The registry reader (VB.net)
I have included an application with the source code. This one will resume all what we have done.
It's like the registry editor of Windows but still view only and can't edit values.
Download the source code
The Zip file contains:
-The sample source code
-The registry viewer application
-The readme.txt file
-The tutorial.txt file
Still have question:
If you have any problem or you found a bug, post a comment describing your problem.
If you have a general question, we highly recommend the MSDN Forums as the best Dot Net forums in the net.
Find out more tutorials at http://thedotnetsource.blogspot.com/
Tutorial 6: How to use (add, read, change, delete) registry keys with vb.net
- Monday, April 21, 2008 - 0
comments
This the most occurred problem. When someone try to install a blogger template, this problem often occur!First I won't talk about the errors but the right method and after it you'll never get errors!Blogger supports 2 templates format.There's 2 templates format that Blogger support.The Classic formatThe New format: XMLThen when you download a template, else you'll find a text file or an XML file.If you found a text file: It's the Classic template.If you found an XML file: It's the new template.Which is better? The new sure why?Because you can arrange and create element easily. However you need to type if you use the old blogger templates.Now we came to install.If the templates is classic.Go to layout > Edit HTML and then select "reverse to classic template"Then copy and paste your codeIf the template is XMLGo to layout > Edit HTML and then click browse and select the XML file then upload it!(Don't copy and paste the XML code)The problem occur? Post a comment?
There's a wonderful widget that you can include on your site.
See I'm not here advertising for Feedjit, but just by founding it that it cost that review.
The widget I use it already, is a traffic widget.
This one has the main features:
You know with it the last 10 visitors. From where they come, from each page, to any page.
It drives traffic for you!
If a visitor redirects to feedjit, the feedjit site has also the widget. and you'll have your site on the widget.
People who will access the site for this period may also access your site.
Feedjit drive about 5 visitors daily for my blog. Also it helps me knowing the best keywords.
You may try it.
See it just near you on the right. click to redirect to the site.
Thks for the traffic ;)
See I'm not here advertising for Feedjit, but just by founding it that it cost that review.
The widget I use it already, is a traffic widget.
This one has the main features:
You know with it the last 10 visitors. From where they come, from each page, to any page.
It drives traffic for you!
If a visitor redirects to feedjit, the feedjit site has also the widget. and you'll have your site on the widget.
People who will access the site for this period may also access your site.
Feedjit drive about 5 visitors daily for my blog. Also it helps me knowing the best keywords.
You may try it.
See it just near you on the right. click to redirect to the site.
Thks for the traffic ;)
Subscribe to:
Posts (Atom)