C # Operations on the registry (online, only for personal use, not original)

Source: Internet
Author: User

Operations on the registry in C #

The Registry of the Windows operating system contains a lot of information about how the computer is running, and opening the registry we can see that the registry is organized in a directory-like tree structure, where the second-level directory contains five predefined primary keys: Hkey_classes_root,hkey_ Current_user,hkey_local_machine,hkey_users,hkey_current_config.

Let's explain the effects of these 5 classes separately.

HKEY_CLASSES_ROOT the primary key contains the file's extension and the application's associated information, as well as the window shell and OLE information used to store the registry. The subkeys under the primary key determine how the class files and their icons are displayed in Windows, and the primary key is mapped from hkey_lccal_machine\software\classes.
HKEY_CURRENT_USER The primary key contains information such as the User's window information, desktop settings, and other current users.

The HKEY_LOCAL_MACHINE primary key contains installation and configuration information for computer software and hardware that can be used by all users
HKEY_USERS the primary key records the current user's settings information, each time the user logs into the system, the primary key will be generated under the same name as the user login subkey, the child key saved the current user's desktop settings, background bitmap, shortcut keys, fonts and other information. The general application does not directly access the primary key, but rather accesses it through the primary keys HKEY_CURRENT_USER.

Hkey_current_config This primary key holds the configuration information for the computer's current hardware, which can be changed depending on the type of network currently connected or the hardware-driven software installation.

C # also supports editing of the registry. The NET Framework provides two classes in the Microsoft.Win32 namespace to manipulate the registry: Registry and RegistryKey. These two classes are sealed classes that are not allowed to be inherited. Let's introduce each of the two classes below.

The Registry class provides 7 public static domains representing 7 basic primary keys (two of which are not in the XP system, In this is not introduced) are: Registry.classesroot,registry.currentuser,registry.localmachine,registry.users,registry.currentconfig. How many keys do they correspond to? I think you'll know when you see it.

The RegistryKey class provides methods for registry operations. Note that the operation registry must comply with system permissions, or an error will be thrown.

Here are a few ways to operate the registry

The method prototypes for creating sub-keys are:


Public RegistryKey CreateSubKey (string sunbkey);


The parameter Sunbkey represents the name or path name of the subkey to be created. Create successfully returns the child key that was created, otherwise null is returned.

The method prototype for opening the child keys is:


Public RegistryKey OpenSubKey (string name);
Public RegistryKey OpenSubKey (string name,bool writable);

The parameter name indicates the subkey name to open or its path name, and the parameter writable indicates whether the subkey opened is allowed to be modified, and the subkey opened by the first method is read-only. The Microsoft.Win32 class also provides us with another way to open the registry on a remote computer, using the following method:


public static RegistryKey OpenRemoteBaseKey (registryhive hkey,string machinename);

The method prototypes for deleting subkeys are:


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 then the delete fails and returns an exception, if you want to completely delete the subkey under the extreme directory of the subkey can be used method Deletesubkeytree, the method prototype is as follows:


public void Deletekeytree (string subkey);

The method prototype for reading the 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, the return type is an object type, or null if the specified key does not exist. If you fail and you do not want the value returned to be null, you can specify the parameter defaultvalue, specifying the parameter to return the value specified by the parameter in the case of a read failure.

The method for setting the key value is as follows:


public Object SetValue (string name,object value);

C # Operations on the registry (online, only for personal use, not original)

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.