Vsiaul C # How to read Registration Information

Source: Internet
Author: User

 

 

Microsoft introduced the registry concept in the Windows system since Windows 95. What is the registry? It 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, the minor cause is that the startup process of the Windows system is abnormal, and the severe case may cause the system to be completely paralyzed. Therefore, correct understanding of the Registry and timely backup of the Registry are very important for Windows users.
Vsiaul C # can easily and concisely develop a program for operating the registry. This article describes how to use VisualC # to read information in the registry.

 

I. Preliminary understanding of the registry:
Click "Start/Run" and enter "Regedit" after "open ". You can see the data structure of the Registry. For example. Note: The Regedit File is a tool provided by Microsoft to edit the registry for users.

Click the thumbnail to zoom in. Figure 01: Registry Structure

For example, the left part is called "primary key" in the registry. As shown in the figure, "primary key" has a hierarchical structure. The next primary key of a primary key is called the "subkey" of the primary key ". Each 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 and each key value have different functions.

Ii. Visual C # how to read the primary key and key value in the registry:
In. NET Framework SDK beta 2, there is a Microsoft. Win32 namespace, which provides two classes for registry operations: Registry class and registrykey class. Both classes are closed classes and cannot be inherited. In these two classes, many methods and attributes about the Registry are defined. By calling these two classes, in Visual C #, you 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 below:
Registry. classesroot corresponds to the 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.

3. Use an example to read the registry information to describe the usage of the two methods:

(1). Environment for program design and operation:
I Windows 2000 Server Edition

Ii. NET Framework SDK beta 2
(2) Some necessary work before running the program:
During program design, the main function is to read the existing primary key values. You can create several primary keys and corresponding key values according to the structure shown in the following figure:

Click the thumbnail to zoom in. Figure 02: information about the registry to be read in the program design.

It is necessary to note that only the key value corresponding to the subkey "new item #3" is displayed. The subkey in "New item #2" also has a key value. The corresponding key value is: "New Value #1" is "001 ", "New Value #2" is "002 ". The key value corresponding to the subkey "new item #1" is: "New Value #1" is "AAA", "new value #2" is "BBB ".
(3) Main Functions of the program:
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 the form of a list in layers. This is the interface after the program runs:

Click the thumbnail to zoom in. Figure 03: The registry information is read and displayed as a list.

(4) important steps in the program design process and some issues that should be paid attention:

The method used to read the primary key, subkey, and key value in the I program:
In the program, four methods in the registrykey class are used to read the subkeys and their values under the specified Primary Key: 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 getvalue (string name) method specifies the key value of a key.
The specific statement used in the program is as follows:
Registrykey HKLM = registry. localmachine;
// Open the "System" subkey
Registrykey software = HKLM. opensubkey ("system ");
// Open the "001" subkey
Registrykey No1 = software. opensubkey ("001 ");
// Open the sub-key "002"
Registrykey NO2 = no1.opensubkey ("002 ");

Listbox1 is the list name defined in the program.
Ii. How to display registration information in the form of a list:
Because the return values of the getsubkeynames () and getvaluenames () methods are string arrays, The foreach statements are used in the program to traverse these string arrays. In addition, the list is displayed during traversal. The specific implementation statements in the program are as follows:
Foreach (string site in no2.getsubkeynames ())
// Start traversing the string array composed of subkeys
{
Listbox1.items. Add (SITE );
// Add a subkey name to the list
Registrykey sitekey = no2.opensubkey (SITE );
// Enable this subkey
Foreach (string svalname in sitekey. getvaluenames ())
// Start to traverse the string array consisting of the key and value names owned by the specified stator key
{
Listbox1.items. Add ("" + svalname + ":" + sitekey. getvalue (svalname ));
// Add the key name and corresponding key value to the list
}
}

(5). Source Code:
Through the above discussion, we can get the program source code, specifically:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using Microsoft. Win32; public class form1: Form
{
Private system. componentmodel. Container components;
Private ListBox listbox1;
Private button button1;
Public form1 ()
{
Initializecomponent ();
}
// Clear resources used in the program
Public override void dispose ()
{
Base. Dispose ();
Components. Dispose ();
}
// Initialize the components used in the program
Private void initializecomponent ()
{
This. components = new system. componentmodel. Container ();
This. button1 = new button ();
This. listbox1 = new ListBox ();
Button1.location = new system. Fig. Point (16,320 );
Button1.size = new system. Drawing. Size (75, 23 );
Button1.tabindex = 0;
Button1.text = "reading the Registry ";
Button1.click + = new system. eventhandler (this. button#click );
Listbox1.location = new system. Drawing. Point (16, 32 );
Listbox1.size = new system. Drawing. Size (496,264 );
Listbox1.tabindex = 1;
This. Text = "reading master test table information ";
This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (528,357 );
This. Controls. Add (this. listbox1 );
This. Controls. Add (this. button1 );
}
Protected void button#click (Object sender, system. eventargs E)
{
Listbox1.items. Clear ();
Registrykey HKLM = registry. localmachine;
Registrykey software = HKLM. opensubkey ("system ");
// Open the "System" subkey
Registrykey No1 = software. opensubkey ("001 ");
// Open the "001" subkey
Registrykey NO2 = no1.opensubkey ("002 ");
// Open the sub-key "002"
Foreach (string site in no2.getsubkeynames ())
// Start traversing the string array composed of subkeys
{
Listbox1.items. Add (SITE );
// Add a subkey name to the list
Registrykey sitekey = no2.opensubkey (SITE );
// Enable this subkey
Foreach (string svalname in sitekey. getvaluenames ())
// Start to traverse the string array consisting of the key and value names owned by the specified stator key
{
Listbox1.items. Add ("" + svalname + ":" + sitekey. getvalue (svalname ));
// Add the key name and corresponding key value to the list
}
}
}
Public static void main ()
{
Application. Run (New form1 ());
}
}

Iv. Summary:
Using Visual C # To Read registration information in the registry is achieved through two classes in the namespace micorsoft. Win32. The two classes also define methods for deleting, modifying, and renaming the registry information. These methods are more destructive and practical than the read and open methods described in this article. The corresponding methods will be introduced in future articles.
Through the above introduction, we found that using Visual C # to process the registry is actually a simple and easy task. Although it is easy, I would like to remind you that, because the registry plays an important role in the Windows system, you must back up the registry before each operation, you must be very careful when performing operations, because every misoperation may cause your system to crash.

 

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.