C # modifies the registry and needs to reference the Microsoft.Win32 namespace
Using Microsoft.Win32; Declaration//quote RegistryKey REG; reg = Registry.classesroot;
The following four cases from ' read ' ' write ' ' delete ' ' judgment ' to implement simple operation on the registry
1. Read the value of the registry with 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);
}
The above is the new XXX directory under the HKEY_LOCAL_MACHINE\Software directory in the registry and in this directory create a registry key with the name value Tovalue;
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);
}
}
The above is the name of the registry key that is removed from the HKEY_LOCAL_MACHINE\Software directory in the registry under the XXX directory;
4. Determine if 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;
}
The above is in the registry in the HKEY_LOCAL_MACHINE\Software directory in the XXX directory to determine whether the name of the registry key exists, this method in the deletion of the registry already exists, in the new registry key should be a corresponding judgment;
C#. NET Modify the Registry