C # Read Registry information

Source: Internet
Author: User
Tags directory create

The registry is a core database of the Windows system, 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, for some reason, the registry is compromised, the light is an exception to the startup process of the Windows system, which can cause the entire system to be completely paralyzed.   So the correct understanding of the registry, timely backup of the registry, for Windows users is very important. C # can be very convenient, concise development of the operation of the registry program. Click Start/Run, and then fill in "regedit" after "open". You can see the data structure of the registry. The "PRIMARY key" is a hierarchical structure. The primary key's next-level primary key is called the "subkey" of the primary key. Each primary key can have more than one child key. The values on the right are the so-called key values. Each primary key or sub-key can have more than one key value.   The registry is a huge database in which each primary key, each key value, is given a different function. How 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 in this namespace: The registry class, the RegistryKey class. These two classes are enclosing classes and cannot be inherited.   These two classes define many of the methods and properties of the registry, and by invoking 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, each representing seven basic primary keys in the Windows registry, as follows: Registry.classesroot corresponds to

HKEY_CLASSES_ROOT PRIMARY key Registry.currentuser corresponds to HKEY_CURRENT_USER primary key Registry.localmachine corresponds to 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 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 its sub-key and to handle the specific operation. Here is an example of reading the registry information to specify the two usage. Programming and running Environment: Windows 2000 Server Edition,. Net FrameWork SDK Beta 2. Some of the necessary processing work before running the program. In the program design, the main function is to read the existing primary key key value, the user can create a new number of primary keys and corresponding key values. The main function of the program is to read the key values owned by all the subkeys and subkeys under the specified primary key, and display them as a list in a hierarchical manner. Important steps in the process of programming and some problems to be aware of: The method used to read the primary key, subkey, and key values in the program: in order to read the key values in the sub-keys and subkeys under the specified primary key, the main use of the four methods in the RegistryKey class: OpenSubKey, Getsubkeynames,getvaluenames,getvalue. The specific usage and meaning are as follows: The OpenSubKey (string name) method is primarily to open the specified subkey. The Getsubkeynames () method is to get the names of all the subkeys under 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 following is a simple operation on the registry from the ' read ' ' write ' ' delete ' ' judgment ' four cases 1. Read the value of the registry with the specified name

<textarea style="width: 703px; height: 176px; line-height: 1.5; font-family: Courier New; font-size: 12px;">private String Getregistdata (string name) {string Registdata; RegistryKey hkml = registry.localmachine; RegistryKey software = hkml. OpenSubKey ("Software", true); RegistryKey Aimdir = software. OpenSubKey ("XXX", true); Registdata = Aimdir. GetValue (name). ToString (); return registdata; }</textarea>The above is the registry value named name in the XXX directory under the HKEY_LOCAL_MACHINE\Software directory in the read registry;

2. Write data to the registry

Private voidWtregedit (string name,string tovalue) {RegistryKey HKLM = Registry.localmachine; RegistryKey software = HKLM. OpenSubKey ("software",true); RegistryKey Aimdir = software. CreateSubKey ("XXX"); Aimdir. SetValue (Name,tovalue); }

The above is the new XXX directory under the HKEY_LOCAL_MACHINE\Software directory in the registry and in this directory create a registry key with the name value Tovalue;

3. Delete the registry key specified in the registry

Private voidDeleteregist (StringName) {String[] aimnames; RegistryKey HKML=Registry.localmachine; RegistryKey Software=Hkml. OpenSubKey ("Software",True); RegistryKey Aimdir = software. OpenSubKey ("XXX",true); Aimnames = aimdir. Getsubkeynames (); foreach(string aimkey in aimnames) { if(aimkey = = name) aimdir. Deletesubkeytree (name); } }

The above is the name of the registry key that is removed from the HKEY_LOCAL_MACHINE\Software directory in the registry under the XXX directory;

4. Determine if the specified registry key exists

Private boolIsregeditexit (StringName) {bool_exit= False;String[] subkeynames; RegistryKey HKML=Registry.localmachine; RegistryKey Software=Hkml. OpenSubKey ("Software",True); RegistryKey Aimdir=Software. OpenSubKey ("Xxx",true=< Span style= "color: #000000;" > Aimdir. Getsubkeynames (); foreachstring KeyName in Subkeynames) { if (keyName == name) {_exit = true< Span style= "color: #000000;" >; return _exit;}} return _exit;}

The above is in the registry in the HKEY_LOCAL_MACHINE\Software directory in the XXX directory to determine whether the name of the registry key exists, this method when deleting the registry already exists, the new registry key should be a corresponding decision.

Original address: http://www.cnblogs.com/A_ming/archive/2011/05/03/2034877.html

C # Read Registry information

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.