|
Windows Registry is a central database for application configuration settings and other information required by the applications. The Windows Registry is a data repository that exists on each computer in Windows Operating systems. Both the system and application programs use this repository to store information needed at runtime. For example, the system stores file associations (the applications or command lines associated with each known file extension) in the Registry. Applications often use the Registry to store information such as users' option settings and data file pathnames.
Note:- If you've never open Windows registry, you can see it by running regedit from command line
.NET Framework Library provides two classes - Registry and RegistryKey to work with the windows registry. These classes are defined in Microsoft.Win32 namespace. So before using these classes, you need to add reference to this namespace.
Registry Class:
The Registry class contains members to provides access to registry keys. We can define registry keys in the following order.
- CurrentUser - Stores information about user preferences.
- LocalMachine - Stores configuration information for the local machine.
- ClassesRoot - Stores information about types (and classes) and their properties.
- Users - Stores information about the default user configuration.
- PerformanceData - Stores performance information for software components.
- CurrentConfig - Stores non-user-specific hardware information.
- DynData - Stores dynamic data.
The Registry class members are described in the following table.
ClassesRoot
|
Returns a RegistryKey type which provides access to HKEY_CLASSES_ROOT key.
|
LocalMachine
|
Returns a RegistryKey type which provides access to HKEY_LOCAL_MACHINE key.
|
CurrentConfig
|
Returns a RegistryKey type which provides access to HKEY_CURRENT_CONFIG key.
|
DynData
|
Returns a RegistryKey type which provides access to HKEY_DYN_DATA key.
|
CurrentUser
|
Returns a RegistryKey type which provides access to HKEY_CURRENT_USER key
|
PerformanceData
|
Returns a RegistryKey type which provides access to HKEY_PERFORMANCE_DATA key.
|
Users
|
Returns a RegistryKey type which provides access to HKEY_USERS key.
|
RegistryKey Class :
The RegistryKey class contains members to add, remove, replace, and read registry data. Some of its common methods and properties are defined in the following table.
Properties:
Name - Represents the name of the key.
SubKeyCount - Represents the count of subkeys at the base level, for theCurrent key.
ValueCount - Represents the count of values in the key.
Methods:
GetValueNames
|
Retrieves an array of strings that contains all the value names associated with this key.
|
GetValue
|
Returns the specified value.
|
OpenSubKey
|
Opens a subkey.
|
GetSubKeyNames
|
Returns an array of strings that contains all the subkey names.
|
DeleteSubKeyTree
|
Deletes a subkey and any children.
|
DeleteSubKey
|
Deletes the specified subkey.
|
CreateSubKey
|
Creates a new subkey if not exists, otherwise opens an existing subkey.
|
SetValue
|
Sets the specified value.
|
DeleteValue
|
Deletes the specified value from a key.
|
Close
|
Closes the key.
|
Example:- Working with Windows Registry.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
RegistryKey key = Registry .LocalMachine.OpenSubKey( "Software" , true );
RegistryKey newkey = key.CreateSubKey( "NewTestKeyAdded" );
newkey.SetValue( "NewTestKeyAdded" , "ValueForThisTestKey" );
MessageBox.Show( "Key/Value is added in the Registry..." );
}
private void button2_Click( object sender, EventArgs e)
{
RegistryKey pRegKey = Registry .LocalMachine;
pRegKey = pRegKey.OpenSubKey( "HARDWARE\\ DESCRIPTION\\System\\FloatingPointProcessor\\0" );
Object valueOfKey = pRegKey.GetValue( "Identifier" );
MessageBox.Show( "Key Value is :: " + valueOfKey);
}
private void button3_Click( object sender, EventArgs e)
{
RegistryKey delKeyValue = Registry .LocalMachine.OpenSubKey( "Software\\" );
delKeyValue.DeleteValue( "ValueForThisTestKey" );
RegistryKey delSubKey = Registry .LocalMachine.OpenSubKey( "Software" , true );
delSubKey.DeleteSubKey( "NewTestKeyAdded" );
MessageBox.Show( "Deleting Key/Value in the Registry" );
}
}
}
Output:

Adding a Key/Value in the Registry

Retrieving Value from the Registry

|