.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 separately. The Registry class provides 7 common static domains, representing 7 basic primary keys (two of which are not available in XP systems):
Registry.ClassesRoot corresponds to the HKEY_CLASSES_ROOT primary key
Registry.CurrentUser corresponds to the HKEY_CURRENT_USER primary key
Registry.LocalMachine corresponds to HKEY_LOCAL_MACHINE primary key
Registry.User corresponds to HKEY_USER primary key
Registry.CurrentConfig corresponds to HEKY_CURRENT_CONFIG primary key
Registry.DynDa corresponds to HKEY_DYN_DATA primary key
Registry.PerformanceData corresponds to the HKEY_PERFORMANCE_DATA primary key RegistryKey class. Note that the operation registry must comply with system permissions, otherwise an error will be thrown. The method for creating a subkey is based on this: public RegistryKey CreateSubKey(string sunbkey); The parameter sunbkey indicates the name or path name of the child key to be created. If the creation is successful, it will return the created subkey, otherwise it will return null. The prototype of the method for opening a subkey is: public RegistryKey OpenSubKey(string name); public RegistryKey OpenSubKey(string name,bool writable); The parameter name indicates the name of the child key to be opened or its path name, the parameter writable indicates whether the opened child key is allowed to be modified, and the child key opened by the first method is read-only. The method of removing subkeys is based on this: public void DeleteSubKey(string subkey); This method is used to remove the specified primary key. If the subkey you want to delete also contains the primary key, the deletion fails and returns an exception, if you want to completely delete the subkey in the extremely directory of the subkey, you can use the method DeleteSubKeyTree, the prototype of the method is as follows: public void DeleteSunKeyTree(string subkey); The prototype of the method for reading 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, returns an object type, and returns null if the specified key does not exist. If the value failed and you don't want to return null, you can specify the parameter defaultValue, and if you specify a parameter, the value specified by the parameter will be returned if the read fails. The prototype of how to set the key values is as follows: public object SetValue(string name,object value); The prototype of how to remove key values is as follows: public void DeleteValue(string name);
Read the registry
|