Working with Windows Registry using VB.NET

Creating a Subkey
Collapse
Collapse
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
regKey.CreateSubKey("MyApp")
regKey.Close()
In the code snippet shown above, I have created a subkey under HKLM\Software called MyApp. Note that I passed True as the second parameter to the OpenSubKey method. This boolean value is to indicate whether the key is writable or not. For instance, you can set it to false if you are just reading data from the registry.
Reading and writing values
Collapse
Collapse
Dim regKey As RegistryKey
Dim ver As Decimal
regKey = Registry.LocalMachine.OpenSubKey("Software\MyApp", True)
regKey.SetValue("AppName", "MyRegApp")
ver = regKey.GetValue("Version", 0.0)
If ver < 1.1 Then
regKey.SetValue("Version", 1.1)
End If
regKey.Close()

In the code snippet shown above, I am creating two values AppName and Version. I am also setting the values to MyRegApp and 1.1 respectively. Note: If you recollect that in the previous sections I had mentioned about data types for registry values like REG_SZ. But nowhere in the above code we mentioned about the data type. This is because .NET runtime interprets the type itself based on what is passed as value and we do not need to pass it explicitly.

Deleting a Subkey
Collapse
Collapse
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("Software", True)
regKey.DeleteSubKey("MyApp", True)
regKey.Close()

In the code snippet shown above, I am deleting the subkey MyApp and all its values. Note that in the call to DeleteSubkey, I have passed a second Boolean argument of True. This means that an exception is thrown when the key to be deleted is not found

0 Comments »

Page Navigation

IndiBlogger Rank

Hit Stats

About Me

My photo
Technology Lover & an IT Professional.

Twitter

http://www.twitterbuttons.com

MeraWindows

Delhi IT Pro

Delhi Devs

IndiBlogger

IE8

IP Address

Friends

AskVG.com
winspark unlockWindows.com techsurface.com

Copyright © 2009-2010 CyberDimensions | Theme Designed by TheBuckmaker | Re-Designed by: Vasu Jain | Home Page Image : Microsoft Windows 7 . | Theme Distributed by Mumbai Mania