Favorite articles from Nios.org (C # operation registry)

xiaoxiao2021-03-06  68

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 small picture to zoom in, Figure 01: Registry structure illustration

As the portion on the left of the figure is called "primary key" in the registry, it can be seen that the "primary key" is a hierarchical structure. 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). Program design and operational environment: i Windows System 2000 Server Edition

II Net Framework SDK Beta 2 (2) Some necessary processing work before running procedures: When the program is designed, the main function is to read the existing primary key value, the user can create a new number according to the structure shown below. Primary key and corresponding key value: Click on the small map to zoom in, Figure 02: Information about the registry to read in the program design

It 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 key values ​​owned by all the sub-keys and subtles below the primary key, and display it according to the list, the following figure is the rear interface of this program:

Click on the small picture to zoom in, Figure 03: Read the registry information and display it in the form of a list

(4). Important steps in the programming process and some issues that should be noted:

The method used by the primary key, subkey, and key value in the i program: In order to read the key values ​​of the sub-keys and sub-keys below the primary key, the four methods in the RegistryKey class are mainly used: 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 Value name consists of strings array {listbox1.items.add (" svalname ": sitekey.getValue (svalname)); // Add key name and corresponding key value in the list}}}}

(5). 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; use system.collections; using system.bomponentmodel; using 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 ();} // used in the program clears Resource public override void dispose () {base.dispose (); Components.Dispose ();} // Initial 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 main 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 button1_Click (object sender, System.EventArgs e) {listBox1.Items.Clear (); RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "SYSTEM"); // open "SYSTEM" subkey RegistryKey no1 = software.OpenSubKey ( "001"); // open "001" subkey RegistryKey no2 = no1.OpenSubKey ("002"); // Open "

002 "Sub-key Foreach (STRING SITE IN NO2.GETSUBKEYNAMES ()) // Start traversing string array {listbox1.Items.add (site) composed of subkey names; // Add subkey name in the list registryKey Sitekey = NO2 .OpenSubKey (Site); // Open this subkey forward (String SvalName In SiteKey.getVALUENAMES ()) // Start traversing string array {listbox1.Items.add by the key value name owned by the specified subkey. Svalname ":" SiteKey.getValue (SvalName)); // Add key name and corresponding key value in the list}}}} public static void main ()}}}} public static void main ()}}. IV. Summary: Use Visual C # to read the registration information in the registry is achieved by the two classes in the namespace MICORSOFT.WIN32. In this two classes, the deletion, modification, and Some methods renamed. These methods are more destructive, but also more destructive, but also more practical than those described in this article. Introducing the introduction of these methods will be done in future articles. We have found that Visual C # to process the registry, in fact, a relatively simple matter. Things are relaxed, but I also want to remind you, because the registry is important in the window system, so every time Before the registry, you must back up, you must be very careful when you operate, because every misoperation can cause your system to crash.

转载请注明原文地址:https://www.9cbs.com/read-109329.html

New Post(0)