Starting with Windows 95, Microsoft has introduced the concept of the registry into the Windows system. What is the registration form? It is a core database of Windows, in which the various parameters related to the system are stored, which directly control the startup of the system, the driver installation information of the hardware and the registration information of various applications running on the Windows system. This means that if the registry is compromised for some reason, the light is that the Windows system startup process is abnormal and the heavy one may cause complete paralysis of the entire system. So the correct understanding of the registry, timely backup of the registry, for Windows users is very important.
Vsiaul C # can be very convenient and concise to develop the procedures for the operation of the registry. This article describes how to use visualc# to read information in the registry.
A Preliminary recognition of the registry:
Click Start/Run, and then fill in "regedit" after "open". You can see the data structure of the registry. The following figure. Note: The regedit file is a tool that Microsoft provides to users to edit the registry.
Click on the small image to enlarge, Figure 01: Registry structure diagram
As the section to the left of the above figure is called a "primary key" in the registry, it is shown that the primary key is hierarchical. The next-level primary key of a primary key is called a subkey of that primary key. Each primary key can have more than one subkey. As the figure shows, these values on the right are called key values. Each primary key or subkey can have multiple key values. The registry is a large database in which each primary key, each key value, is given a different function.
Two How Visual C # reads the primary key and key values in the registry:
In the. Net FrameWork SDK Beta 2, there is a Microsoft.Win32 namespace that provides two classes for registry operations: Registry class, RegistryKey class. These two classes are all enclosing classes and cannot be inherited. In these two classes, many of the methods and attributes about the registry are defined, and by calling these two classes, it is easier to handle various operations on the Registry in Visual C #.
(1). Registry class:
This class mainly encapsulates seven public static domains, which represent the seven basic primary keys in the window registry, as follows:
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 Heky_current_config primary key
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 mainly encapsulates the basic operation of the Windows system registry. In the program design, first through the registry class to find the basic primary key in the registry, and then through the RegistryKey class, to find the following subkeys and handle specific operations.
Three Use a sample of the registry information to specify the two uses:
(1). Programming and Operating environment:
I Windows System 2000 Server Edition
II Net FrameWork SDK beta version 2
(2) Some of the necessary processing work before running the program:
In programming, the main function is to read the existing primary key value, the user can follow the structure shown in the following figure to create a new number of primary keys and corresponding key values:
Click on the thumbnail to enlarge, figure 02: Information about the registry to be read in program design
It is important to note that the previous figure shows only the key values for the "New Item #3" subkey. In the "New Item" button also has a key value, the corresponding key value is: "New value" for "001", "New value" for "002". The corresponding key value in the "New Item #" subkey is: "New value" is "AAA", "New Value" is "BBB".
(3). The main function of the program:
The main function of the program is to read the key values owned by all subkeys and subkeys below the specified primary key, and display them as a list, as shown in the following image:
Click on the thumbnail to enlarge, figure 03: Read the registry information and display it in a list form
(4). Important steps in the process of programming and some problems to be noted:
The method used to read the primary key, subkey, and key values in the I program:
In the program to read the key values in the subkeys and subkeys below the specified primary key, four methods in the RegistryKey class are mainly used: Opensubkey,getsubkeynames,getvaluenames,getvalue. The specific usage and meaning are as follows:
The OpenSubKey (string name) method primarily opens the specified subkey.
The Getsubkeynames () method is to obtain the name of all subkeys below the primary key, and its return value is an array of strings.
The GetValueNames () method is to get all the key names in the current subkey, and its return value is also an array of strings.
The GetValue (string name) method is the key value of the specified key.
The specific use statements in the program are as follows:
RegistryKey HKLM = Registry.localmachine;
Open the SYSTEM subkey
RegistryKey software = HKLM. OpenSubKey ("SYSTEM");
Open "001" subkey
RegistryKey no1 = software. OpenSubKey ("001");
Open "002" subkey
RegistryKey NO2 = No1. OpenSubKey ("002");
Where ListBox1 is the name of the list defined in the program.
II How to display registration information in a list form:
Because the return value of the Getsubkeynames () method and the GetValueNames () method is an array of strings, it is implemented through a foreach statement in the program to iterate through the array of strings. And in the traversal, it is displayed through the list form, the specific implementation of the program statement is as follows:
foreach (string site in NO2. Getsubkeynames ())
Begins traversing a string array of child key names
{
LISTBOX1.ITEMS.ADD (site);
Add a subkey name to the list
RegistryKey Sitekey = No2. OpenSubKey (site);
Open this subkey
foreach (String svalname in Sitekey. GetValueNames ())
Begins traversing a string array of key-value names owned by the specified subkey
{
ListBox1.Items.Add ("" + Svalname + ":" + Sitekey. GetValue (Svalname));
Add a key name and corresponding key value in the list
}
}
(5). SOURCE Program code:
Through the above discussion, we can get the program source code, specific as follows:
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 the resources used in the program
public override void Dispose ()
{
Base. Dispose ();
Components. Dispose ();
}
Components used in the initialization program
private void InitializeComponent ()
{
this.components = new System.ComponentModel.Container ();
This.button1 = new Button ();
This.listbox1 = new ListBox ();
Button1. Location = new System.Drawing.Point (16, 320);
Button1. Size = new System.Drawing.Size (75, 23);
Button1. TabIndex = 0;
Button1. Text = "Read Registry";
Button1. Click + + new System.EventHandler (This.button1_click);
Listbox1.location = new System.Drawing.Point (16, 32);
Listbox1.size = new System.Drawing.Size (496, 264);
Listbox1.tabindex = 1;
This. Text = "Read the main 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 Button1_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 "001" subkey
RegistryKey NO2 = No1. OpenSubKey ("002");
Open "002" subkey
foreach (string site in NO2. Getsubkeynames ())
Begins traversing a string array of child key names
{
LISTBOX1.ITEMS.ADD (site);
Add a subkey name to the list
RegistryKey Sitekey = No2. OpenSubKey (site);
Open this subkey
foreach (String svalname in Sitekey. GetValueNames ())
Begins traversing a string array of key-value names owned by the specified subkey
{
ListBox1.Items.Add ("" + Svalname + ":" + Sitekey. GetValue (Svalname));
Add a key name and corresponding key value in the list
}
}
}
public static void Main ()
{
Application.Run (New Form1 ());
}
}
Four Summarize:
Using Visual C # to read registration information in the registry is implemented through two classes in the namespace Micorsoft.win32. Some of the methods for deleting, modifying, and renaming registry information are also defined in these two classes. These methods are more destructive, but also more practical, than the read and open methods described in this article. An introduction to these methods will be made in future articles.
Through the above introduction, we found that the use of Visual C # to deal with the registry, in fact, is a relatively easy and simple things. Although it is easy, I would also like to remind you that because of the important role of the registry in the Windows system, make sure to back up every time you work on the registry, and be very careful when you do it, because every mistake you take can cause your system to crash.
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.