C # operation registry (transfer)

xiaoxiao2021-03-05  23

The registry of the Windows operating system contains a lot of configuration information about the computer, open the registry. We can see that the registry is organized according to the tree structure similar to the directory, where the second level directory contains five predefined primary keys, respectively. : HKEY_CLASS_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG.

Below we will explain the role of these five classes, respectively.

HKEY_CLASSES_ROOT This primary key contains file extensions and application information and information for Window Shell and OLE to store registry. The subkey under this primary key determines how to display this class file and their icon in Windows, which is mapped from HKEY_LCCAL_MACHINE / SOFTWARE / CLASSES.

HKEY_CURRENT_USER This primary key contains information such as user window information, desktop settings and other current users.

The HKEY_LOCAL_MACHINE key key contains computer software and hardware installation and configuration information, which allows all users to use HKEY_USERS that primary key records the current user's setup information. When the user logs into the system, it will generate a login under the primary key. The same subkey, the subkey saves the current user's desktop settings, background bitmaps, shortcuts, fonts and other information. The general application does not directly access the transder key, but is accessed by the primary key HKEY_CURRENT_USER.

HKEY_CURRENT_CONFIG This primary key holds configuration information of the computer current hardware, which can be changed according to the currently connected network type or the change in hardware drive software installation.

C # also supports the editing of the registry, .NET Framework provides two classes in Microsoft.Win32 namespaces to operate registry: Registry and RegistryKey. Both classes are sealing and is not allowed to be inherited. Let's introduce these two classes separately.

The Registry class provides 7 public static domains, representing 7 basic primary keys (two of which are not in the XP system, this is not introduced), respectively: Registry.classesRoot, Registry.Currentuser, Registry.localMachine, Registry , Registry.currentconfig. Which keys should be corresponding to what other keys, I will know it.

A method for registry operation is provided in the RegistryKey class. It should be noted that the operational registry must meet system authority, otherwise an error will be thrown.

Let's take a few ways to operate registry.

The prototype of creating a subkey:

Public RegistryKey CreateSubKey (String Sunbkey);

The parameter sunbkey indicates the name or path name of the subkey to be created. Create successfully returns a subkey created, otherwise returns NULL.

The prototype of the open sub-key is:

Public RegistryKey OpenSubkey; PUBLIC RegistryKey OpenSubKey (String Name, Bool Writable)

The parameter name indicates that the sub-key name or its path name you want to open, and the parameter WRITABLE indicates whether the started subkey is allowed to be modified, and the subkey that is only read only by the first method. Microsoft.Win32 class also provides us with another method to open registry on the remote computer, the prototype:

Public Static RegistryKey OpenRemoteBaseKey (RegistryHive HKey, String Machinename);

The prototype of the delete subkey is:

Public void deleteKey; This method is used to delete the specified primary key. If the subkey to be deleted, the delete failed, and returned an exception. If you want to completely remove the sub-keys in the extreme directory of the child, you can use the method deleteSubkeyTree, the prototype is as follows:

Public void deleteKeyTree (String Subkey);

The prototype of the read key value is as follows:

Public Object getValue (String name); public object getValue (String Name, Object Default ";

The name of the parameter name indicates the key, the return type is an Object type, and NULL is returned if the specified key does not exist. If the value that the failure does not want to return is NULL, you can specify parameter defaultValue, specifying parameters, returns the value specified by the parameter without reading failure.

The prototype of the setting key value is as follows:

Public Object SetValue (String Name, Object Value);

转载请注明原文地址:https://www.9cbs.com/read-36103.html

New Post(0)