C read and write registry ideas and code

  • 2020-05-24 06:05:04
  • OfStack

The NET framework provides two classes in the Microsoft.Win32 namespace to manipulate the registry: Registry and RegistryKey. Both classes are hermetically sealed and are not allowed to be inherited. Let's take a look at each of these classes.
The Registry class provides seven common static fields, each representing the seven basic primary keys (two of which are not in the XP system) :
Registry.ClassesRoot corresponds to the HKEY_CLASSES_ROOT primary key
Registry.CurrentUser corresponds to the HKEY_CURRENT_USER primary key
Registry.LocalMachine corresponds to the HKEY_LOCAL_MACHINE primary key
Registry.User corresponds to HKEY_USER primary key
Registry.CurrentConfig corresponds to the HEKY_CURRENT_CONFIG primary key
Registry.DynDa corresponds to the HKEY_DYN_DATA primary key
Registry.PerformanceData corresponds to HKEY_PERFORMANCE_DATA primary key
The RegistryKey class provides methods to operate on the registry. It is important to note that operating the registry must comply with system permissions, otherwise an error will be thrown.

The method prototype for creating a subkey is:
public RegistryKey CreateSubKey(string sunbkey);
The parameter sunbkey represents the name or pathname of the child key to be created. Creates successfully returns the created child key, otherwise returns null.
The prototype 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, and the parameter writable indicates whether the opened subkey is allowed to be modified. The subkey opened by the first method is read-only.
The prototype method for deleting subkeys is:
public void DeleteSubKey(string subkey);
This method is used to delete the specified primary key. If the subkey to be deleted also contains the primary key, the deletion fails and an exception is returned. If the subkey and the subkey in the directory are to be deleted completely, the method DeleteSubKeyTree can be used. The prototype of the method is as follows:
public void DeleteSunKeyTree(string subkey);
The prototype method for reading the key value is as follows:
public object GetValue(string name);
public object GetValue(string name,object defaultValue);
The parameter name represents the name of the key, with a return type of 1 object or null if the specified key does not exist. You can specify the parameter defaultValue if you fail and don't want to return a value of null, and if you do, return the value specified for that parameter in the case of a read failure.
The prototype method for setting the key value is as follows:
public object SetValue(string name,object value);
The prototype method for deleting key values is as follows:
public void DeleteValue(string name);
The following is a small test case written by myself, running without errors.
1. Write


try
    {
       RegistryKey rsg = null;
       if (Registry.LocalMachine.OpenSubKey("SOFTWARE\\RING").SubKeyCount <= 0)
       {
          Registry.LocalMachine.DeleteSubKey("SOFTWARE\\RING");
          Registry.LocalMachine.CreateSubKey("SOFTWARE\\RING");
       }
       rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\RING",true);//true Means that it can be modified 
       //if (rsg.GetValue("TestReg") == null)
       //{
       //    rsg.CreateSubKey("TestReg");
       //}
       //else
       //{
       //    rsg.DeleteSubKey("TestReg");               
       //}
       rsg.SetValue("TestReg", System.DateTime.Now.ToString());
       rsg.Close();
       }
  catch (Exception ex)
      {
         this.label2.Text = ex.Message;
      }

2. Read


try
    {
       RegistryKey rsg = null;
       rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\RING",true);
       if (rsg.GetValue("TestReg") != null) // Read failed return null
       {
          this.label2.Text = rsg.GetValue("TestReg").ToString();
       }
       else
          this.label2.Text = " The key does not exist! ";
       rsg.Close();
    }
 catch (Exception ex)
    {
       this.label2.Text = ex.Message;
    }


Related articles: