Use Visual C # to create modified registration information

Source: Internet
Author: User
Tags numeric value

In this article, we describe ways to modify key values in the registry. For primary keys and subkeys, however, there is no way to define this in the. Net FrameWork SDK, so it is not possible to complete the secure modification of the information in the registry. Here's how to create registration information in Visual C #.

A Two methods that Visual C # creates and modifies registration information to invoke:

(1). CreateSubKey (String key) method: This method is to create a subkey with the following string as the name. Of course, this method not only creates subkeys, but also creates a primary key in the program described below.

(2). SetValue (string name, String keyvalue) Method: This method has two points, a numeric value that can be used to rename the key value, and one that can be used to create a new key value. The details are as follows: When this key value is present in the Open subkey, the new value is assigned to him and the rename operation is implemented. If it does not exist, a new key value is created.

Two Programming and operating environment and the work to be prepared:

I> Windows System 2000 Server Edition

Ii>.net FrameWork SDK Beta version 2

Iii> because in the program, you want to modify an existing key value, so you need to set the key value in the position. Open the Registry Editor, create a "AAA" subkey under the "Hardware" subkey under the "HKEY_LOCAL_MACHINE" key, and create a key value called "BBB" under this subkey.

Three The main functions of the program and the important steps of the design:

In the program described below, the main function is to create a primary key, a subkey, and modify a specified key value in the registry using Visual C #. The structural hierarchy of the subkeys to be created is under the primary key "Hkey_local_machin" under the "Hareware" primary key, the name is "DDD", which contains a key value, the name is "www", and the value of the key value is "1234".

The structural hierarchy of the primary key to be created is also under the "Hareware" primary key under the primary key "Hkey_local_machin", the name "main" contains a subkey named "Sub" under this primary key and the name is "value", and the value of the key value is "1234 "。 Here's how Visual C # creates and modifies these primary keys, subkeys, and key values.

(1). How to create a subkey, which is implemented in the program in conjunction with the CreateSubKey () method and the SetValue () method, which is the source program in which the subkeys are created:

listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE" , true ) ;
RegistryKey main1 = software.CreateSubKey ( "main" ) ;
RegistryKey ddd = main1.CreateSubKey ( "sub" ) ;
ddd.SetValue ( "value" , "1234" );

(2). How to create a primary key, the process of creating a primary key and creating a subkey is roughly the same. Because a primary key contains several subkeys, you must be aware of their hierarchical relationships when creating a primary key. The following section of the program, in reference to the time, please note the relationship between the main key and the sub key.

listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey main1 = software.CreateSubKey ( "main" ) ;
RegistryKey ddd = main1.CreateSubKey ( "sub" ) ;
ddd.SetValue ( "value" , "1234" ) ;

(3). How to modify the registration information. Because the information in the registry is important, it is generally not written. This may also be the reason that there is no way to modify a registry key in the. Net FrameWork SDK. Instead, it provides a relatively small method of--setvalue (), through which we can modify the value of the key. The following program code is a key value that modifies a specified key value name. Of course, because of the nature of the SetValue () method, if it detects that the key value does not exist, a new key value is created.

listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ;
dddw.SetValue ( "bbb" , "abcd" ) ;

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.