| RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打开"aaa"子键 RegistryKey no2 = no1.OpenSubKey ( "bbb" , true ) ; //打开"bbb"子键 no2.DeleteValue( "ccc" ) ; //删除名称为"ccc"的键值 |
| RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打开"aaa"子键 no1.DeleteSubKey ( "bbb", false ); //删除名称为"bbb"的子键 |
| RegistryKey hklm = Registry.LocalMachine ; hklm.DeleteSubKey ( "aaa", false ); RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打开"aaa"子键 no1.DeleteSubKeyTree ( "ddd" ); //删除名称为"ddd"的子键 |
| 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 ( ) ; } //清除在程序中使用过的资源 public override void Dispose ( ) { base.Dispose ( ) ; components.Dispose ( ) ; } //初始化程序中使用到的组件 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 = "读取注册表" ; 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 = "删除键值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 = "删除子键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 = "删除主键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 = "用Visual C#来删除注册表中的主键、子键和键值!" ; 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" ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa" ) ; //打开"aaa"子键 foreach ( string site in no1.GetSubKeyNames ( ) ) //开始遍历由子键名称组成的字符串数组 { listBox1.Items.Add ( site ) ; //在列表中加入子键名称 RegistryKey sitekey = no1.OpenSubKey ( site ) ; //打开此子键 foreach ( string sValName in sitekey.GetValueNames ( ) ) //开始遍历由指定子键拥有的键值名称组成的字符串数组 { listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ; //在列表中加入键名称和对应的键值 } } } protected void button2_Click ( object sender , System.EventArgs e ) { RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打开"aaa"子键 RegistryKey no2 = no1.OpenSubKey ( "bbb" , true ) ; //打开"bbb"子键 no2.DeleteValue( "ccc" ) ; //删除名称为"ccc"的键值 } protected void button3_Click ( object sender , System.EventArgs e ) { RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打开"aaa"子键 no1.DeleteSubKey ( "bbb", false ); //删除名称为"bbb"的子键 } protected void button4_Click ( object sender , System.EventArgs e ) { RegistryKey hklm = Registry.LocalMachine ; hklm.DeleteSubKey ( "aaa", false ); RegistryKey software = hklm.OpenSubKey ( "SOFTWARE", true ) ; //打开"SOFTWARE"子键 RegistryKey no1 = software.OpenSubKey ( "aaa", true ) ; //打开"aaa"子键 no1.DeleteSubKeyTree ( "ddd" ); //删除名称为"ddd"的子键 } public static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } } |