Visual C # To delete registration information in the registry

Source: Internet
Author: User
Tags foreach delete key win32
visual| Registration Form

In the reading of registration information in Visual C #, you have described using Visual C # to read registry information in the registry. This article describes another operation with Visual C # for the registry, which is also a destructive process--deleting registration information.

As already known, because Visual C # itself does not have a class library, his processing of the registry is accomplished by invoking the two classes encapsulated in the namespace Microsoft.Win32 in the. Net FrameWork SDK. These two classes are registry class, RegistryKey class. Three methods are defined in the RegistryKey class to delete registration information in the registry. They are: DeleteSubKey () method, Deletesubkeytree () method, DeleteValue () method. Here's a concrete description of how to use these three methods correctly in Visual C #.

A How to call these three methods in Visual C #:
Before you explain how to use these three methods, you need to RegistryKey a method--opensubkey () method in the class. As described in the previous article, this method is to open the specified subkey. In fact, the OpenSubKey () method has two ways of calling:

I >. OpenSubKey (String, subkey): This invocation is done only for read operations on this subkey.
II >. OpenSubKey (String subkey, Boolean writable): Use this method to invoke a write operation on a child key. If you use a write operation on a child key, but you still use the first invocation method, an error message is generated when the program runs.

(1). DeleteSubKey () Method:
This method deletes a specified subkey, and when this method is used, an error message is generated if there are additional subkeys in this subkey. There are two prototypes of calling this method in a program:
I >. DeleteSubKey (String, subkey): This invocation is simply to delete the specified subkey.

II >. DeleteSubKey (String subkey, Boolean info): where "string" is the name of the subkey to delete, the Boolean argument means: If the value is "True", the deleted subkey does not exist when the program is called, the Give birth to an error message; If the value is "False", the deleted subkey does not exist and no error message is generated when the program is called, and the program still runs correctly. So in the specific program design process, I still recommend the use of the second method of invocation.

(2). Deletesubkeytree () Method:
This method is to completely delete the specified subkey directory, that is, delete the subkey and all subkeys below the subkey. Because the destructive power of this method is very strong, all are important to use. A prototype that calls this method in a program is:

Deletesubkeytree (String subkey): Where "subkey" is the name of the subkey to be completely removed.

(3). DeleteValue () Method:
This method deletes the specified key value. A prototype that calls this method in a program is:
DeleteValue (String value): where "value" is the name of the key value to be deleted.
After introducing the methods for registering information in the registry, a program will be used to explain their specific usage in the program.

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 the function of the program is to delete the specified primary key, subkey, and key values, this requires that we first set the location and name of the values in the registry. Specifically as follows:
The following subkey and key values are established in the "SOFTWARE" subkey below the HKEY_LOCAL_MACHINE PRIMARY key:
Create a "AAA" subkey under the "SOFTWARE" subkey. Under AAA subkey, establish the "BBB" subkey and the "DDD" subkey. In the "BBB" subkey to establish the name "CCC" key value, the value of the key value is "CCC." The child "DDD" subkey establishes the subkey "Eee" and establishes a "FFF" key value in this subkey, and the value of the key value is "FFF". In the program to delete the key value is "CCC" key value, to delete the subkey is "BBB", to completely delete the subkey is "DDD." Specify the following figure:

Click on the thumbnail to enlarge Figure 01: The Registry chart for the program

Three Important steps in Programming:
The main step in programming is to delete key values, subkeys that do not contain any subkeys, and subkeys that contain subkeys. The following procedures are used to specify:
(1). How to delete a key value. In the program to delete the key value is "CCC." The following is the specific statement in the program that deletes this key value.
RegistryKey HKLM = Registry.localmachine;
RegistryKey software = HKLM. OpenSubKey ("SOFTWARE", true);
Open the "SOFTWARE" subkey
RegistryKey no1 = software. OpenSubKey ("AAA", true);
Open "AAA" subkey
RegistryKey NO2 = No1. OpenSubKey ("BBB", true);
Open "BBB" subkey
No2. DeleteValue ("CCC");
Delete a key value with the name "CCC"


(2). How to delete subkeys that do not contain any subkeys. In the program to delete the subkey is "BBB". Here is the specific program code to delete this subkey:
RegistryKey HKLM = Registry.localmachine;
RegistryKey software = HKLM. OpenSubKey ("SOFTWARE", true);
Open the "SOFTWARE" subkey
RegistryKey no1 = software. OpenSubKey ("AAA", true);
Open "AAA" subkey
No1. DeleteSubKey ("BBB", false);
Delete a subkey with the name "BBB"


(3). How to delete subkeys that contain subkeys. The subkey that you want to delete in your program is DDD. Here is the specific program code to delete this subkey:
RegistryKey HKLM = Registry.localmachine;
Hklm. DeleteSubKey ("AAA", false);
RegistryKey software = HKLM. OpenSubKey ("SOFTWARE", true);
Open the "SOFTWARE" subkey
RegistryKey no1 = software. OpenSubKey ("AAA", true);
Open "AAA" subkey
No1. Deletesubkeytree ("ddd");
To delete a subkey with the name "DDD"


Four The program source code (Reg.cs) in this article and the Run interface:
The main function of the Reg.cs program is to delete key values in the registry, subkeys that do not contain subkeys, and subkeys that contain subkeys. And through the button "read the registry", the list of the display method to timely understand the deletion. The following diagram is the interface after the program is run:

Click to enlarge Figure 02: The Running interface of the program in this article

Reg.cs Program source code is 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 ();
}
Clear the resources used in the program
public override void Dispose ()
{
Base. Dispose ();
Components. Dispose ();
}
Components used in the initialization program
private 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 sub-key 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 primary keys, subkeys, 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" subkey
foreach (String site in No1. Getsubkeynames ())
Begins traversing a string array of child key names
{
LISTBOX1.ITEMS.ADD (site);
Add a subkey name to the list
RegistryKey Sitekey = no1. OpenSubKey (site);
Open this subkey
foreach (String svalname in Sitekey. GetValueNames ())
Begins traversing a string array of key-value names owned by the specified subkey
{
ListBox1.Items.Add ("" + Svalname + ":" + Sitekey. GetValue (Svalname));
Add a key name and corresponding key value in the list
}
}
}
protected void button2_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" subkey
RegistryKey NO2 = No1. OpenSubKey ("BBB", true);
Open "BBB" subkey
No2. DeleteValue ("CCC");
Delete a key value with 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" subkey
No1. DeleteSubKey ("BBB", false);
Delete a subkey with the name "BBB"
}
protected void Button4_Click (object sender, System.EventArgs e)
{
RegistryKey HKLM = Registry.localmachine;
Hklm. DeleteSubKey ("AAA", false);
RegistryKey software = HKLM. OpenSubKey ("SOFTWARE", true);
Open the "SOFTWARE" subkey
RegistryKey no1 = software. OpenSubKey ("AAA", true);
Open "AAA" subkey
No1. Deletesubkeytree ("ddd");
To delete a subkey with the name "DDD"
}
public static void Main ()
{
Application.Run (New Form1 ());
}
}


Five Summarize:
This article describes an important aspect of Visual C # Registry programming: How to delete registration information. Because deleting registration information is a very destructive operation, it is important to pay attention to the protection of the Registry before you operate.




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.