The following describes how to perform simple operations on the registry from the four examples of 'read' and ''delete ''.
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;
String [] subkeyNames;
RegistryKey hkml = Registry. LocalMachine;
RegistryKey software = hkml. OpenSubKey ("SOFTWARE", true );
RegistryKey aimdir = software. OpenSubKey ("XXX", true );
SubkeyNames = aimdir. GetSubKeyNames ();
Foreach (string keyName in subkeyNames)
{
If (keyName = name)
{
_ Exit = true;
Return _ exit;
}
}
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.