Starting from Windows 95, Microsoft introduces the concept of registry in the window system. What is the registry in the end? It is a core database of the window system, stores various parameters related to the system in this database, direct control of the system's startup, hardware driver installation information, and various applications running on the window system Registration information, etc. This means that if the registry is destroyed because some reasons are damaged, the light is abnormal, and the heavy person may lead to completeness of the entire system. So the correct understanding of the registry, timely backup registry, is quite important for window users. VSIAUL C # can be very convenient and concise to develop procedures for operating registry. This article describes how to use VisualC # to read the information in the registry. One. Preliminary Understanding Registration: Click Start / Run, fill in "regedit" behind "Open". You can see the data structure of the registry. As shown below. Note: The regedit file is a tool for Microsoft to edit the registry to the user. Click on the vap into larger, Figure 01: The registry structure illustrates the part of the left side of the figure in the registry "primary key", according to the figure, the "primary key" is hierarchical. The next primary key of the primary key is called the "subkey" of the primary key. Each primary key can be paired with multiple sub-keys. As shown, the values on the right is the so-called key value. Each primary key or subkey can have multiple key values. The registry is a huge database that gives each key, each key value gives different functions. two. Visual C # How to read the primary keys and key values in the registry: In the .NET Framework SDK Beta 2, there is a namespace of Microsoft.Win32, which provides two classes for registry operations in this namespace: Registry class, RegistryKey class. These two classes are closed and cannot be inherited. In these two classes, many methods and properties about the registry are defined, by calling these two classes, in Visual C # can be relaxed to process the various operations of the registry. (1) .registry Class: This type mainly encapsulates seven public static domains, and these static domains represent seven basic primary keys in this window registry, specifically as follows: registry.classesroot corresponds to the HKEY_CLASSES_ROOT primary key registry .CurrentUser corresponding primary key HKEY_CURRENT_USER HKEY_LOCAL_MACHINE Registry.LocalMachine corresponding to the primary key Registry.User HKEY_USER corresponding to the primary key corresponding to HEKY_CURRENT_CONFIG Registry.CurrentConfig Registry.DynDa primary key corresponding to the primary key Registry.PerformanceData HKEY_DYN_DATA HKEY_PERFORMANCE_DATA corresponding primary key (2) .RegistryKey: This series The basic operation of the window system registry is mainly encapsulated. In programming, first find the basic primary key in the registry via the Registry class, and then via the RegistryKey class to find the following sub-keys and processing specific operations.
three. These two usage are specified by an example of reading registry information: (1). Programming and running environment: i Window System 2000 Server Edition II Net Framework SDK Beta 2 (2) Before running Some necessary processing work: When the program is designed, the main function is to read the existing primary key value, and the user can create a new number of primary keys and the corresponding key value according to the structure shown below: Click on the small picture to enlarge, Figure 02: The information to be read in the program design is necessary to explain that the above figure shows only the key value corresponding to the "new item # 3" subkey. In the "new item # 2" subkey, there is a key value, the corresponding key value is: "New Value # 1" is "001", "New Value # 2" is "002". The corresponding key value in the "New Item # 1" sub-key is: "New Value # 1" is "AAA", "New Value # 2" is "BBB". (3). The main function of the program: The main function of the program is to read all the sub-keys and sub-keys under the specified primary key, and display it in the form of the list, the following figure is the process of running the program: Click Small map enlarges, Figure 03: Read the registry information and display it in the form of a list (4). The important steps in the program design process and some of the issues should be noted: the primary key, sub-key and key value are read in the i program. Method: In order to read the key values of the sub-keys and sub-keys below the primary key, the program is mainly used in the RegistryKey class: OpenSubKey, GetSubKeynames, GetValuenames, getValue. The specific usage and meaning are as follows: OpenSubKey (String Name) method is mainly to open the specified subkey. The getSubKeynames () method is to get the name of all sub-keys under the main key, and its return value is a string array. The getValuenames () method is to get all the names in the current subkey, and its return value is also a string array. GetValue (String Name) method is the key value of the specified key. The specific usage statement in the program is as follows: registryKey hklm = registry.localMachine; // Open "System" sub-key registryKey Software = hklm.opensubkey ("system"); // Open "001" sub-key registryKey no1 = Software.Opensubkey "001"); // Open "002" subkey registryKey no2 = no1.opensubkey ("002"); where ListBox1 is the list name defined in the program. How to display registration information in the form of a list: Due to the return value of the getSubkeyNames () method, the return value of the getValueEnames () method is a string array, it is used to translate these string arrays through the Foreach statement.
And when traversing, it is displayed in the form of a list, and the specific implementation statements in the program are as follows: Foreach (String Site in no2.getsubkeyNames ()) // Start traversing the string array {listbox1.Items.Add composed of the sub-key name {listbox1.items.add ( Site); // Add a subkey name in the list registryKey Sitekey = no2.opensubKey (Site); // Open this subkey forward (String SvalName In SiteKey.getVALUENAMES ()) // Start traversing the key owned by the specified subkey A string array {listbox1.items.add (" svalname ": sitekey.getValue (svalname)); // add key name and corresponding key value} (5) in the list. Source Code: Through the above discussion, we can get the source code code of the program, which is as follows: use system; use system.drawing; using system.collections; using system.componentmodel; use system.windows.form; using system.data ; using Microsoft.Win32; public class Form1: Form {private System.ComponentModel.Container components; private ListBox listBox1; private Button button1; public Form1 () {InitializeComponent ();} // remove the used public override resources in the program Void Dispose (); Components.Dispose (); Components.Dispose ();} // Initializer Used Components Private Void InitializationComponent () {this.components = new system.componentmodel.container (); this.butto N1 = 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 test table information"; this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = New System.drawing.size (528, 357);