C # Registry Operation implementation Code _c# tutorial

Source: Internet
Author: User
Tags win32
The second level directory contains five predefined primary keys, respectively: Hkey_classes_root,hkey_current_user,hkey_local_machine,hkey_users,hkey_current_config.

Let's explain the role 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 to display the class files and their icons in Windows, which are mapped from the hkey_lccal_machine\software\classes.
HKEY_CURRENT_USER This primary key contains information such as User window information, desktop settings, and so on for the current user.

The HKEY_LOCAL_MACHINE primary key contains installation and configuration information for computer software and hardware that is available to all users
HKEY_USERS This primary key records the current user's settings information, and each time the user logs on to the system, a subkey is generated under the primary key that is the same as the user's login name, which holds the current user's desktop settings, background bitmap, shortcut keys, fonts, and so on. The general application does not directly access the modifier primary key, but accesses it through the primary key HKEY_CURRENT_USER.

Hkey_current_config This primary key saves configuration information about the current hardware of your computer, which can be changed depending on the type of network you are currently connected to or the hardware-driven software installation changes.

C # also supports edits to the registry. 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. Here are the two classes that we'll introduce separately.

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

A method for registry operations is provided in the RegistryKey class. Note that the operation registry must conform to system permissions, or an error will be thrown.

Here are a few ways to do this. Several common methods of operation registry

The method for creating subkeys is:


Public RegistryKey CreateSubKey (string sunbkey);



The parameter Sunbkey represents the name or path name of the subkey to create. Creates a successful return of the created subkey, otherwise null is returned.

The method for opening subkeys is:


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


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


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


The method for deleting a subkey is:


public void DeleteKey (string subkey);


This method is used to delete the specified primary key. If you want to delete a subkey that also contains a primary key, the deletion fails and an exception is returned, and if you want to completely delete the subkey in the extremely directory of the subkey, you can use the method Deletesubkeytree, which is the following prototype:


public void Deletekeytree (string subkey);


The method prototype for reading the key value 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, and returns NULL if the specified key does not exist. If you fail and you do not want the returned value to be null, you can specify the parameter defaultvalue, and the parameter returns the value specified by the parameter if the read fails.

The method prototype for setting the key value is as follows:


public Object SetValue (string name,object value);

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.