Thursday, 18 April 2013

Accessing The Registry Key With VB.Net.


Introduction Of Registry Key
 Programmer/We have always found the windows registry to be a suitable place for storing
 application specific information and configuration settings. Traditionally, the registry has
 been used for storing configuration information like database connection strings,text, profiles
 etc. The popularity of the registry can be attributed to the fact that registry access is faster
 than file access and also because it is a very secure system-wide data repository.For creating
 a registry key, the 'RegistryKey' key world is used, this registry key is present in Win32 package.

Prerequisites for work with registry-
1.Familiarity with previous versions of Visual Basic.
2.Knowledge of the design and purpose of the registry
3.Understanding of the security implications of accessing the registry.

Use the Vb function for accessing Registry Key-

Create a new registry-
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
This line will create a key in the HKEY_CURRENT_USER hive and will be named "Testkey".
For the value we'll need three things: Value path, Value name and value value 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.")

Read the value from registry key-
Dim readValue As String
readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\TestKey", 
"TestValue", Nothing)
MsgBox("The value is " & readValue)

Delete the registry and registry's value-
My.Computer.Registry.CurrentUser.DeleteSubKey("TestKey")
Delete the Value-
readValue.DeleteValue("Testvalue") {readValue is define just above}

Change the registry value-
readValue.SetValue("TestValue", RegValue, RegistryValueKind.String)
readValue.SetValue("name of key value",give the value,value type)

Count the number of KeysValue and subKey in hive-
My.Computer.Registry.CurrentUser.ValueCount.ToString()
For subKey-
My.Computer.Registry.CurrentUser.SubKeyCount.ToString()

Check if a value exist-
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\TestKey",
"TestValue", Nothing) Is Nothing Then
MsgBox("Value does not exist.")
Else
MsgBox("Value exist.")
End If

Now Write Here A Small Program for Create Registry-

I create a small application for outlook,here is small part off that module-
Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Tools.Outlook
Imports System.IO
Imports System.IO.Directory
Imports Microsoft.Win32

Private Sub SettingsForm_Load(ByVal sender As System.Object, 
                                                ByVal e As System.EventArgs) Handles MyBase.Load
        Dim TestRegKey As RegistryKey
        Dim AccountRegVal As String
        Dim EmailRegVal As String
        Dim PasswordRegVal As String
        AccountRegVal = AccountTextBox.Text.ToString
        EmailRegVal = EmailTextBox.Text.ToString
        PasswordRegVal = PasswordTextBox.Text.ToString
        TestRegKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\Outlook
                                \Addins\TestValue", True)
       TestRegKey.SetValue("Account", AccountRegVal, RegistryValueKind.String)
       TestRegKey.SetValue("Email", EmailRegVal, RegistryValueKind.String)
       TestRegKey.SetValue("Password", PasswordRegVal, RegistryValueKind.String)
       TestRegKey.Close()
       MsgBox(" Connection Saved")

End Sub




No comments:

Post a Comment