C # registry operation implementation code

Source: Internet
Author: User

The level-2 directory contains five pre-defined primary keys: hkey_classes_root, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, and hkey_current_config.

Next we will explain the functions of these five classes respectively.

Hkey_classes_root this primary key contains the file extension and applicationProgramWindow shell and OLE are used to store registry information. The subkeys under the primary key determine how to display the files and their icons in Windows. The primary key is mapped from hkey_lccal_machine \ Software \ Classes.
HKEY_CURRENT_USER this primary key contains information about the current user, such as user window information and desktop settings.

The HKEY_LOCAL_MACHINE primary key contains the installation and configuration information of computer software and hardware, which can be used by all users.
HKEY_USERS this primary key records the settings of the current user. Each time a user logs on to the system, a subkey with the same name as the user's login name is generated under this primary key, the sub-key stores the current user's desktop settings, background bitmap, shortcut keys, fonts, and other information. Generally, applications do not directly access and change the primary key, but access through the primary key HKEY_CURRENT_USER.

Hkey_current_config the primary key stores the configuration information of the current hardware of the computer. These configurations can be changed based on the network type currently connected or the installation of the hardware driver software.

C # also supports editing the Registry. the. NET Framework provides two classes in the Microsoft. Win32 namespace to operate the Registry: Registry and registrykey. Both classes are sealed and cannot be inherited. Next we will introduce these two classes respectively.

The Registry class provides seven public static domains, which represent seven basic primary keys (two of which are not in the XP system and are not described here): Registry. classesroot, registry. currentuser, registry. localmachine, registry. users, registry. currentconfig. Which keys they correspond.

Registrykey class provides methods for registry operations. Note that the Registry must comply with system permissions; otherwise, an error will be thrown.

Below are some common methods to operate the registry.

The method prototype for creating a sub-key is:

Public registrykey createsubkey (string sunbkey );

The sunbkey parameter indicates the name or path name of the child key to be created. The created sub-key is returned. Otherwise, null is returned.

The prototype of the sub-key is as follows:

Public registrykey opensubkey (string name );
Public registrykey opensubkey (string name, bool writable );

The name parameter indicates the name or path name of the subkey to be opened. The writable parameter indicates whether the subkey to be opened can be modified. The subkey opened in the first method is read-only. The Microsoft. Win32 class also provides us with another method to open the registry on a remote computer. The prototype of the method is:

Public static registrykey openremotebasekey (registryhive hkey, string machinename );

The method prototype for deleting a sub-key is:

Public void deletekey (string subkey );

This method is used to delete a specified Primary Key. If the sub-key to be deleted also contains a primary key, the deletion fails and an exception is returned. If you want to completely delete the sub-key under the sub-key's extreme directory, you can use deletesubkeytree, the method is prototype as follows:

Public void deletekeytree (string subkey );

The following is a prototype of the key value reading method:

Public object getvalue (string name );
Public object getvalue (string name, object defaultvalue );

The parameter name indicates the key name. The return type is an object type. If the specified key does not exist, null is returned. If you do not want the returned value to be null, you can specify the defaultvalue parameter. If you specify a parameter, the value specified by this parameter is returned if the read fails.

The following is a prototype for setting the key value:

Public object setvalue (string name, object value );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.