Http://www.cnblogs.com/whitetiger/archive/2008/04/09/1144219.html
1. Read the registry value of the specified name
Private string GetRegistData (string name)
{
String registData;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("XXX", true );
RegistData = aimdir. GetValue (name). ToString ();
Return registData;
}
The above is the registry value named name in the XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE directory in the read registry;
2. Write Data to the Registry
Private void WTRegedit (string name, string tovalue)
{
RegistryKey hklm = Registry. LocalMachine;
RegistryKey software = hklm. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. CreateSubKey ("XXX ");
Aimdir. SetValue (name, tovalue );
}
In the registry, create a new XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE Directory and create a registry entry named "tovalue" under the directory;
3. Delete the registry key specified in the registry.
Private void DeleteRegist (string name)
{
String [] aimnames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("XXX", true );
Aimnames = aimdir. GetSubKeyNames ();
Foreach (string aimKey in aimnames)
{
If (aimKey = name)
Aimdir. DeleteSubKeyTree (name );
}
}
In the Registry, the XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE Directory deletes the name registry key;
4. Determine whether the specified registry key exists
Private bool IsRegeditExit (string name)
{
Bool _ exit = false;
Try
{
String [] subkeyNames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("Weather", true );
SubkeyNames = aimdir. GetValueNames ();
Foreach (string keyName in subkeyNames)
{
If (keyName = name)
{
_ Exit = true;
Return _ exit;
}
}
}
Catch
{}
Return _ exit;
}
In the Registry, the XXX directory under the HKEY_LOCAL_MACHINE \ SOFTWARE Directory determines whether the registry name is name or not. This method already exists when you delete the registry, when creating a registry entry, the corresponding judgment should also be made;
Example:
using Microsoft.Win32;
using System.Diagnostics;
private void Access_Registry()
{
// Create a new key under HKEY_LOCAL_MACHINE \ Software and name it MCBInc
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// Add a subkey
RegistryKey newkey = key.CreateSubKey("MCBInc");
// Set the value of this subkey
newkey.SetValue("MCBInc", "NET Developer");
// Obtain data from other places in the Registry
// Find your CPU
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
Object val = pRegKey.GetValue("VendorIdentifier");
Debug.WriteLine("The central processor of this machine is:"+ val);
// Delete the key value
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true);
delKey.DeleteSubKey("MCBInc");
}