In "How to Read Registration Information" and "Registration Information in Remove the Registration Information in Visual C #, you have explored that the registration information in the registry in the registry, in this article, in this article. To introduce another two important operations of Visual C # registry programming: Create registration information and modify registration information.
In the second article, we already know that because Visual C # itself does not have class, he is the operation of the registry in some classes defined in the .NET Framework SDK (Software Development Pack) in the .NET Framework. This is the two classes of the package in Microsoft.Win32: Registry class, RegistryKey class. Two methods are defined in the RegistryKey class to create primary keys, sub-keys, and key values in the registry. They are createSubValue () methods and setValue () methods. So how to use Visual C # to modify the registration information, in this article, we only introduce the method of modifying the key value in the registry. For primary keys and sub-keys, since the .NET Framework SDK has not defined this method, it is not possible to complete the information in the secure modification registry. Let's first describe how to create registration information in Visual C #.
One. Visual C # Create and modify the two methods to call the registration information: (1) .createSubkey: This method is a subkey that creates a list of strings in the latter string. Of course, this method can not only create a sub-key, but in the program described below, it also creates a primary key.
(2) .SetValue (String Name, String KeyValue) Method: This method has two points, one can be used to rename key values, one can be used to create a new key value. The specific situation is as follows: When the open subkey, if this key value exists, the new value is assigned to him and rename the operation. If you don't exist, create a new key value.
two. Program design and operating environment and the work to be prepared: i> Window System 2000 Server Edition
II> .NET Framework SDK Beta 2
III> Due to the program, you want to modify a key value already existing, the location of the key value is set in advance. Open the editor of the registry, create a "AAA" subkey under the "HKY_LOCAL_MACHINE" primary key below and create a key value name "BBB" under this subkey. Specifically, as shown below:
Figure 01: Structure of the registry set for the program
three. The main functions of the program and the design of the design: In the program described below, the main function is to create a primary key, a sub-key, and modify a specified key value in the registry in the registry. The structural hierarchical hierarchy of the sub-key to be created is the name "DDD" under the "HareWare" primary key below "HKEY_LOCAL_MACHIN", which contains a key value, name "WWW", 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 below the primary key "HKEY_LOCAL_MACHIN", the name "main", where the primary key contains a sub-key and name "Value" key. Value, the value of the key value is "1234". Here, you will focus on how Visual C # is how to create and modify these primary keys, sub-keys, 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. The following is the source program for creating sub-keys in the program: 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, create a primary key and create a subkey process is roughly almost the same. Since the primary key contains a few sub-keys, they must pay attention to their hierarchical relationship when creating the primary key. Below this section, please note the relationship between the main key and the SUB key when a reference.
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. Since the information in the registry is important, do not write on it. It may also be this reason, and there is no way to modify the registry key in the .NET Framework SDK. Just provide a relatively small method -SetValue (), through this method, we can modify the key value. The following program code is a key value that modifies a specified key value name. Of course, due to the characteristics of the setValue () method, if it detects that this 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");
four. The source program code (REG.CS) and the compiled program run interface: The following is the interface of the program run:
Figure 02: Program operation interface described herein
The REG.CS program code is as follows:
Using system; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using microsoft.win32; // Imported namespace
public class Form1: Form {private System.ComponentModel.Container components; private ListBox listBox1; private Button button1; private Button button2; private Button button3; private Button button4; public Form1 () {InitializeComponent ();} // clear the program Used Resources Public Override Void Dispose () {base.dispose (); Components.Dispose ();} // Initializer Used Component Void InitializationComponent () {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 (90, 23 Button1.tabindex = 0; button1.text = "Read Registry"; Button1.Click = New System.EventHandler (this.button1_click);
This.button2 = new button (); button2.Location = new system.drawing.point (116, 320); button2.size = new system.drawing.size (90, 23); button2.TabINDEX = 1; button2.text = "Create a subkey"; Button2.Click = new system.eventhandler (this.button2_click);
This.button3 = new button (); button3.Location = new system.drawing.point (216, 320); button3.size = new system.drawing.size (90, 23); button3.tabindex = 2; button3.text = "Create primary key"; button3.click = new system.eventhandler (this.button3_click);
This.button4 = new button (); button4.location = new system.drawing.point (316, 320); button4.size = new system.drawing.size (90, 23); button4.tabindex = 3; button4.text = "Rename key value"; Button4.click = new system.eventhandler (this.button4_click);
Listbox1.location = new system.drawing.point (16, 32); listbox1.size = new system.drawing.size (496, 264); listbox1.tabindex = 4; this.text = "Use Visual C # to create and modify Registration information in the registration table! "; This.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (528, 357); // Add component this in the form .Controls.add (this.listbox1); this.controls.add (this.button1); this.controls.add (this.button2); this.controls.add (this.button3); this.controls.add (This.Add (this .Button4);} // Show "Hardware" in the list, "Hardware" below, the key value protected, key value protected, {listbox1.items.clear (); registryKey hklm = registry.localMachine RegistryKey Software = HKLM.OpenSubKey ("hardware"); // Open "System" sub-key Foreach (String Site in Software.getSubKkeyNames // Start traversing the string array {listbox1.Items.add composed of sub-key name {listbox1.items.add (Site); // Add a subkey name in the list RegistryKey Sitekey = Software.OpenSubKey (Site); // Open this key forward (String SvalName In Sitekey.GetValuenames ()) // Start traversing by the specified sub-key Key value name composed of strings array {listbox1.items.add ( "" Svalname ":" SiteKey.getValue (SvalName)); // Add key name and corresponding key value}}} // in the list}} // creates a subkey and key value protected void button2_click (Object Sender, System. EventArgs e) {listBox1.Items.Clear (); RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true); RegistryKey ddd = software.CreateSubKey ( "ddd"); ddd.SetValue ( " www "," 1234 ");} // Create a primary key and creates a key protected void button3_Click (object sender, System.EventArgs e) {listBox1.Items.Clear (); RegistryKey hklm = Registry.LocalMachine; RegistryKey software = HKLM.OpenSubKey ("Hardware", True;