NET framework provides two classes in the Microsoft.Win32 namespace to manipulate the registry: Registry and RegistryKey. Both classes are sealed classes that are not allowed to be inherited. Let’s introduce these two categories below.
The Registry class provides 7 public static domains, representing 7 basic primary keys (two of which do not exist in XP systems) are:
Corresponding to HKEY_CLASSES_ROOT primary key
Corresponding to HKEY_CURRENT_USER primary key
Corresponding to HKEY_LOCAL_MACHINE primary key
Corresponding to HKEY_USER primary key
Corresponding to HEKY_CURRENT_CONFIG primary key
Corresponding to HKEY_DYN_DATA primary key
Corresponding to HKEY_PERFORMANCE_DATA primary key
The RegistryKey class provides methods for operating the registry. It should be noted that the operation of the registry must comply with system permissions, otherwise an error will be thrown.
The prototype of the method to create a subkey is:
public RegistryKey CreateSubKey(string sunbkey);
The parameter sunbkey represents the name or path name of the child key to be created. Creation successfully returns the created subkey, otherwise null is returned.
The prototype of the method to open the subkey is:
public RegistryKey OpenSubKey(string name);
public RegistryKey OpenSubKey(string name,bool writable);
The parameter name indicates the name of the subkey to be opened or its path name, the parameter writable indicates whether the opened subkey is allowed to be modified, and the subkey opened by the first method is read-only.
The prototype of the method to delete subkeys is:
public void DeleteSubKey(string subkey);
This method is used to delete the specified primary key. If the subkey you want to delete also contains the primary key, the deletion fails and an exception is returned. If you want to completely delete the subkey in the directory, you can use the method DeleteSubKeyTree. The prototype of this method is as follows:
public void DeleteSunKeyTree(string subkey);
The prototype of the method to read key values is as follows:
public object GetValue(string name);
public object GetValue(string name,object defaultValue);
The parameter name represents the name of the key. The return type is an object type. If the specified key does not exist, it returns null. If the value returned fails and does not want to be null, you can specify the parameter defaultValue. If the parameter is specified, the value specified by the parameter is returned in the event of a failed read.
The prototype of the method to set key values is as follows:
public object SetValue(string name,object value);
The prototype of the method to delete key values is as follows:
public void DeleteValue(string name);
Below is a test example written by me myself, and there are no errors in running.
1. Write
try
{
RegistryKey rsg = null;
if (("SOFTWARE\\RING").SubKeyCount <= 0)
{
("SOFTWARE\\RING");
("SOFTWARE\\RING");
}
rsg = ("SOFTWARE\\RING",true);//true means that it can be modified
//if (("TestReg") == null)
//{
// ("TestReg");
//}
//else
//{
// ("TestReg");
//}
("TestReg", ());
();
}
catch (Exception ex)
{
this. = ;
}
2. Read
try
{
RegistryKey rsg = null;
rsg = ("SOFTWARE\\RING",true);
if (("TestReg") != null) //Return null if read failed
{
this. = ("TestReg").ToString();
}
else
this. = "This key does not exist!";
();
}
catch (Exception ex)
{
this. = ;
}