Use C # To 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, 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.
C # You can easily and concisely develop a program to operate the registry. Click "Start/Run" and 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 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.
C # how to read the primary key and key value in the registry. net Framework SDK in 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. These two Classes define many methods and attributes for the Registry. By calling these two classes, you can easily process various operations on the registry in Visual C.
(1). Registry class: This class encapsulates seven public static domains, which represent the seven basic primary keys in the Windows Registry respectively, 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. the user corresponds to the hkey_user primary key registry. currentconfig corresponds to the primary key registry of heky_current_config. 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:
The method used to read the primary key, subkey, and key value in the program: to read the key values of the subkey under the specified primary key, it mainly uses four methods in the registrykey class: 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" sub-key registrykey software = HKLM. opensubkey ("system"); // open the "001" subkey registrykey No1 = software. opensubkey ("001"); // open the "002" subkey registrykey NO2 = no1.opensubkey ("002 ");

Listbox1 is the list name defined in the program.

Foreach (string site in no2.getsubkeynames () // start to traverse the string array consisting of subkey names {listbox1.items. add (SITE); // Add the subkey name registrykey sitekey = no2.opensubkey (SITE) to the list; // open this subkey foreach (string svalname in sitekey. getvaluenames () // start to traverse the String Array {listbox1.items. add ("" + svalname + ":" + sitekey. getvalue (svalname); // Add the key name and corresponding key value to the list }}

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 the resources used in the program public override void dispose () {base. dispose (); components. dispose ();} // 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. button#click); listbox1.location = new system. drawing. point (16, 32); listbox1.size = new system. drawing. size (496,264); listbox1.tabindex = 1; this. TEXT = "reading information about the main test table"; 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 "002" subkey foreach (string site in no2.getsubkeynames () // start to traverse the string array consisting of the subkey name {listbox1.items. add (SITE); // Add the subkey name registrykey sitekey = no2.opensubkey (SITE) to the list; // open this subkey foreach (string svalname in sitekey. getvaluenames () // start to traverse the String Array {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 ());}}

Using 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 registry plays an important role in the Windows system. Therefore, you must back up the registry before performing any operation. Be careful when operating the registry, A misoperation may cause system 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.