Home >Backend Development >C#.Net Tutorial >C# registry operation implementation code
The second-level directory contains five predefined primary keys: HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, and HKEY_CURRENT_CONFIG.
Let’s explain the functions of these 5 classes respectively
HKEY_CLASSES_ROOT This primary key contains the file extension and application related information, as well as the information used by Window Shell and OLE to store the registry. The subkeys under this primary key determine how to display this type of files and their icons in WINDOWS. This primary key is mapped from HKEY_LCCAL_MACHINESOFTWAREClasses.
HKEY_CURRENT_USER This primary key contains information about the current user such as user window information, desktop settings, etc.
HKEY_LOCAL_MACHINE primary key contains the installation and configuration information of computer software and hardware. This information is available to all users.
HKEY_USERS primary key records the current user’s setting information. Every time the user logs in to the system, a primary key will be generated under this primary key. A subkey that is the same as the user's login name. This subkey saves the current user's desktop settings, background bitmap, shortcut keys, fonts and other information. Generally, applications do not directly access the primary key, but access it through the primary key HKEY_CURRENT_USER.
HKEY_CURRENT_CONFIG This primary key saves the current hardware configuration information of the computer. These configurations can be changed according to the type of network currently connected or the installation of hardware driver software.
C# also supports editing of the registry. The .NET framework provides two classes in the Microsoft.Win32 namespace to operate the registry: Registry and RegistryKey. Both classes are sealed classes and are not allowed to be inherited. Below we introduce these two classes respectively.
The Registry class provides 7 public static fields, representing 7 basic primary keys respectively (two of which are not in the XP system and will not be introduced here): Registry.ClassesRoot, Registry.CurrentUser, Registry.LocalMachine , Registry.Users, Registry.CurrentConfig. I think you will know which keys they correspond to at a glance.
The RegistryKey class provides methods for operating the registry. It should be noted that operating the registry must comply with system permissions, otherwise an error will be thrown.
Now let’s take a look at some commonly used methods to operate the registry
The prototype of the method to create a subkey is:
public RegistryKey CreateSubKey(string sunbkey);
The parameter sunbkey represents the subkey to be created name or pathname. If the creation is successful, the created subkey is returned, otherwise null is returned.
The prototype of the method to open a subkey is:
public RegistryKey OpenSubKey(string name);
public RegistryKey OpenSubKey(string name, bool writable);
The parameter name represents 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. The subkey opened by the first method is read-only. The Microsoft.Win32 class also provides us with another method for opening the registry on the remote computer. The method prototype is:
public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, string machineName);
Method for deleting subkeys The prototype is:
public void DeleteKey(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 will fail and an exception will be returned. If you want to completely delete the subkey and the subkeys in the directory, you can use the method DeleteSubKeyTree. The method prototype is as follows:
public void DeleteKeyTree(string subkey ; An object type that returns null if the specified key does not exist. If it fails and you do not want the returned value to be null, you can specify the parameter defaultValue. If a parameter is specified, the value specified by the parameter will be returned if the reading fails.
The prototype of the method for setting key values is as follows:
public object SetValue(string name,object value);
The above is the content of the C# registry operation implementation code. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!