Use Visual C # to delete registration information in the registry

xiaoxiao2021-03-06  73

In the article "Read Registration Information" with Visual C #, you have already introduced the registration information in the registry in the registry. This article describes another operation with Visual C # to the registry, which is also a destructive operation process - delete registration information.

It is already known above, because the Visual C # itself does not have a class library, his processing of the registry is achieved by calling the two classes encapsulated in the Namespace in the .NET Framework SDK. These two classes are the Registry class, the RegistryKey class. Three ways are defined in the RegistryKey class to delete registration information in the registry. They are: deleteSubKey () method, deleteSubkeyTree () method, deletevalue () method. The following is specifically described how these three methods are used correctly in Visual C #.

One. How to call these three methods in Visual C #: You also need to re-introduce one method in the RegistryKey class before describing how to use these three methods. In the previous article, this method is to open the specified subkey. In fact, the OpenSubKey () method has two ways to call:

I> .OpenSubKey (String, Subkey): This call mode is just a read operation for this subkey. II> .OpenSubKey (String Subkey, Boolean Writable): Use this call method when the pair key is written. If the write operation is used in the pair subkey, it still uses the first call method, and an error message will be generated when the program is run.

(1). DeleteSubkey () method: This method is to delete a specified subkey. When using this method, if there is another subkey in this subkey, an error message is generated. There are two prototypes to: i>. DeleteSubKey (String, Subkey): This call mode is to delete the specified subkey directly.

Ii>. DeleteSubkey (String Subkey, Boolean Info): The "String" is the name of the subkey to be deleted, the "Boolean" parameter means: If the value is "true", then when the program is called, delete The child does not exist, then generate an error message; if the value is "false", the deleted subkey does not exist when the program is called, and the program is still correct. Therefore, in the specific programming process, I recommend using the second call method.

(2). DeleteSubkeyTree () method: This method is to completely delete the specified subkey directory, ie: Delete the sub-key and the entire sub-key below the subkey. Since the destructiveness of this method is very strong, all is very important when using it. The prototype of this method is called in the program, for:

DELETESUBKEYTREE (STRING Subkey): where "Subkey" is the subkey name to completely delete.

(3). DeleteValue () method: This method is to delete the specified key value. The prototype of this method in the program is: DeleteValue: where "value" is the name of the key value to be deleted. After introducing the method of registration information in the registry, they will be used to specifically usage in the program through a program. two. Program design and operational environments and the work to be prepared: i>. Windows System 2000 Server Edition

II> ..NET Framework SDK Beta 2

Iii>. Since the functionality is to delete the specified primary key, sub-key and key value, this requires us to set the position and name of these values ​​in the registry. Specifically, the "Software" subkey under the HKEY_LOCAL_MACHINE key key is established: "AAA" sub-key is established under the "Software" sub-key. The "AAA" subrunction is established under the "BBB" subkey and "DDD" subkey. The key value of the name "CCC" is established in the "BBB" subkey, and the value of the key value is "CCC". The sub-key "EEE" is created in the "DDD" sub-key and establishes a "FFF" key value in this subkey, and the value of the key value is "FFF". The key value to be deleted in the program is the "CCC" key value. The subkey to be deleted is "BBB", and the subkey to completely delete is "DDD". The specific setting is shown below:

Click on the small map to enlarge Figure 01: Registry structure diagram set for the program

three. Important steps for programming: The main steps of programming are how to delete key values, and do not contain any sub-keys of any sub-keys, and contain sub-keys. The following will be specifically described below: (1). How to delete the key value. The key value to be deleted in the program is "CCC". The following is a specific statement that deletes this key value in the program. RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true); // Open the "SOFTWARE" subkey RegistryKey no1 = software.OpenSubKey ( "aaa", true); // open "aaa" sub Key RegistryKey NO2 = NO1.OPENSUBKEY ("BBB", true); // Open "BBB" subkey no2.deletevalue ("CCC"); // Delete key values ​​named "CCC"

(2). How to delete a subkey that does not contain any sub-keys. The subkey to be deleted is "BBB". The following is to remove this subkey specific program code: RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true); // Open the "SOFTWARE" subkey RegistryKey no1 = software.OpenSubKey ( "aaa" , TRUE); // Open "AAA" sub-key NO1.DELETESUBKEY ("BBB", false); // Delete a subkey name "BBB"

(3). How to delete a sub-key that contains a subkey. This subkey to be deleted in the program is "DDD". The following is the specific program code that deletes this subkey: registryKey hklm = registry.localmachine; hklm.deleteSubkey; registryKey Software = HKLM.OpenSubKey ("Software", true); // Open "Software" Key REGISTRYKEY NO1 = SOFTWARE.OPENSUBKEY ("aaa", true); // Open "AAA" sub-key no1.deleteSubkeyTree ("DDD"); // Delete the subkey four name "DDD". The program source code (REG.CS) and the run interface: the main function of the REG.CS program is to delete key values ​​in the registry, the sub-keys of the sub-key and the sub-keys containing the sub-key. And by the button "Read Registry", the display method is deleted in a list of display methods. The following figure is the interface after the program runs:

Click on the small map to enlarge Figure 02: Run interface of the program in this article

reg.cs source code as follows: 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; private Button button2; private Button button3; private Button button4; public Form1 () {InitializeComponent ();} // remove the used resources in the program public override void Dispose () {base.dispose (); components.dispose ();} // Initializer Used Component Void InitializeComponent () {Components = New System.comPonentModel.Container (); button1 = new button (); Button2 = new button (); button3 = new button (); button4 = new button (); 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 (Button1_click);

Button2.location = new system.drawing.point (116, 320); button2.size = new system.drawing.size (75, 23); button2.tabindex = 0; button2.text = "Delete key value CCC"; button2 .Click = new system.eventhandler (Button2_Click); Button3.Location = new system.drawing.point (216, 320); button3.size = new system.drawing.size (75, 23); button3.tabindex = 0; Button3.text = "Delete subkey BBB"; Button3.Click = New System.EventHandler (Button3_Click);

Button4.location = new system.drawing.point (316, 320); button4.size = new system.drawing.size (75, 23); button4.tabindex = 0; button4.text = "Delete Primary Key DDD"; button4. Click = New System.EventHandler (Button4_Click);

Listbox1.location = new system.drawing.point (16, 32); listbox1.size = new system.drawing.size (496, 264); listbox1.tabindex = 1;

THIS.TEXT = "Use Visual C # to delete the primary keys, sub-keys and key values ​​in the registry!"; this.autoscalebasesize = new system.drawing.size (5, 13); this.clientsize = new system.drawing.size (528, 357); this.controls.add (listbox1); this.controls.add (button1); this.controls.add (button2); this.controls.add (Button3); this.controls.add (Button4) ;} protected void button1_Click (object sender, System.EventArgs e) {listBox1.Items.Clear (); RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE"); // open the "SOFTWARE" subkey RegistryKey NO1 = Software.OpensubKey ("aaa"); // Open "AAA" sub-key Foreach (String Site in no1.getsubkeyNames ()) // Start traversing string array {listbox1.Items.add composed of subkey name {listbox1.items.add ( Site); // Add a subkey name in the list registryKey Sitekey = NO1.OPENSUBKEY (SITE); // Open this key forward (String SvalName In SiteKey.getVALUENAMES ()) // Start traversing the key owned by the specified subkey Value name consisting of strings array {listbox1.items.add (" svalname ": sitekey.getvalue (svalname)); // Add key name and corresponding key value in the list}}}} protected void button2_click (Object sender, system.eventargs e) {RE gistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true); // Open the "SOFTWARE" subkey RegistryKey no1 = software.OpenSubKey ( "aaa", true); // open "aaa" sub Key RegistryKey No2 = NO1.OPENSUBKEY ("BBB", true); // Open "BBB" sub-key no2.deletevalue ("ccc"); // Remove the key value of the name "CCC"} protected void button3_click (Object Sender , System.EventArgs e) {RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true); // open the "SOFTWARE" subkey RegistryKey no1 = software.OpenSubKey ( "aaa", true);

// Open "AAA" sub-key NO1.DELETESUBKKEY ("BBB", FALSE); // Remove the subkey name "BBB"} protected void button4_click (object sender, system.eventargs e) {registryKey hklm = registry.localmachine HKLM.DELETESUBKKEY ("aaa", false); registryKey Software = HKLM.OpenSubKey ("Software", true); // Open "Software" sub-key registryKey no1 = Software.OpensubKey ("aaa", true); //// Open "AAA" sub-key no1.deleteSubkeyTree ("DDD"); // Delete sub-keys named "DDD"} public static void main () {Application.Run (New Form1 ());}} 5. Summary: This article introduces an important part of Visual C # registry programming, namely: how to delete registration information. Since deleting registration information is a very destructive operation, you must pay attention to the protection of the registry before operation.

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

New Post(0)