C # registry operations

Source: Internet
Author: User

The Windows operating system registry contains many configuration methods for computer running. Open the registry and we can see that the Registry is organized by a tree structure similar to a directory, 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 application association information, and Window 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_MACHINESOFTWAREClasses.

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.