C # Read Registry Information

Source: Internet
Author: User

The Registry is a core database of the Windows system, in which various system-related parameters are stored, these parameters directly control the system startup, hardware driver installation information, and registration information of various applications running on the Windows system. This means that if the registry is damaged for some reason, if the Startup Process of the Windows system encounters an exception, the severe loss may cause the entire system to crash completely. Therefore, correct understanding of the Registry and timely backup of the Registry are very important for Windows users.
C # You can easily and concisely develop a program to operate the registry. Click Start
/Run. Enter "Regedit" after "open ". You can see the data structure of the Registry. The "primary key" has a hierarchical structure. The next primary key of a primary key is called the "subkey" of the primary key ". Each
A primary key can have multiple subkeys. The values on the right are the so-called key values. Each primary key or subkey can have multiple key values. The Registry is a huge database, in which each primary key, each
Each key value has different functions.
C # how to read the primary key and key value in the Registry: In. NET Framework SDK Beta
In version 2, there is a Microsoft. Win32 namespace, which provides two classes for registry operations: Registry class, registrykey
Class. Both classes are closed classes and cannot be inherited. These two Classes define many methods and attributes about the Registry. By calling these two classes
C # can easily process various operations on the registry.

(1 ). registry class: This class encapsulates seven public static domains, which represent the seven basic primary keys in the Windows registry, as shown in the following code: Registry. classesroot corresponds

Hkey_classes_root primary key
Registry. currentuser corresponds to the HKEY_CURRENT_USER primary key
Registry. localmachine corresponds to the HKEY_LOCAL_MACHINE primary key
Registry. User corresponds to the hkey_user primary key
Registry. currentconfig corresponds to the primary key of heky_current_config
Registry. dynda corresponds to the hkey_dyn_data primary key
Registry. performancedata corresponds to the hkey_performance_data primary key.

(2). registrykey class: This class encapsulates basic operations on the Windows registry. In the program design, first find the basic primary key in the registry through the registry class, and then use the registrykey class to find the subkeys and process specific operations.
The following uses an example to read the registry information to describe the usage of these two methods.
Programming and running environment: Windows 2000 Server version,. NET Framework SDK beta 2 version.
Some necessary work before running the program. During program design, the main function is to read existing primary key values. You can create several primary keys and corresponding key values.
The main function of the program is to read the key values owned by all the sub-keys and sub-keys under the specified primary key, and display them in a list in layers. Important steps in the process of program design and some issues that should be paid attention:
Cheng
The method used to read primary keys, subkeys, and key values in sequence:
In the program, to read the key values of the subkeys and subkeys under the specified primary key, the four parties in the registrykey class are used.
Method: opensubkey, getsubkeynames, getvaluenames, and getvalue. The usage and meaning are as follows:
Opensubkey (string name) is used to open a specified subkey.
The getsubkeynames () method is used to obtain the names of all subkeys under the primary key. The returned value is a string array.
The getvaluenames () method is used to obtain all the key names in the current subkey, and its return value is also a string array.

The following describes how to perform simple operations on the registry from the four examples of 'read' and ''delete ''.
1. Read the registry value of the specified name

Private string getregistdata (string name)
{
String registdata;
Registrykey hkml = registry. localmachine;
Registrykey software = hkml. opensubkey ("software", true );
Registrykey aimdir = software. opensubkey ("XXX", true );
Registdata = aimdir. getvalue (name). tostring ();
Return registdata;
}

The above is the registry value named name in the xxx directory under the HKEY_LOCAL_MACHINE \ SOFTWARE directory in the read registry;

2. Write Data to the Registry

Private void wtregedit (string name, string tovalue)
{
Registrykey HKLM = registry. localmachine;
Registrykey software = HKLM. opensubkey ("software", true );
Registrykey aimdir = software. createsubkey ("XXX ");
Aimdir. setvalue (name, tovalue );
}

In the registry, create a new XXX directory under the HKEY_LOCAL_MACHINE \ Software Directory and create a registry entry named "tovalue" under the directory;

3. Delete the registry key specified in the registry.

Private void deleteregist (string name)
{
String [] aimnames;
Registrykey hkml = registry. localmachine;
Registrykey software = hkml. opensubkey ("software", true );
Registrykey aimdir = software. opensubkey ("XXX", true );
Aimnames = aimdir. getsubkeynames ();
Foreach (string aimkey in aimnames)
{
If (aimkey = Name)
Aimdir. deletesubkeytree (name );
}
}

In the Registry, the xxx directory under the HKEY_LOCAL_MACHINE \ Software Directory deletes the name registry key;

4. Determine whether the specified registry key exists

Private bool isregeditexit (string name)
{
Bool _ exit = false;
String [] subkeynames;
Registrykey hkml = registry. localmachine;
Registrykey software = hkml. opensubkey ("software", true );
Registrykey aimdir = software. opensubkey ("XXX", true );
Subkeynames = aimdir. getsubkeynames ();
Foreach (string keyname in subkeynames)
{
If (keyname = Name)
{
_ Exit = true;
Return _ exit;
}
}
Return _ exit;
}

In the Registry, the xxx directory under the HKEY_LOCAL_MACHINE \ Software Directory determines whether the registry name is name or not. This method already exists when you delete the registry, when creating a registry entry, you should also make a judgment accordingly.

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.