分类

  • 软件天地

  • 用C#来删除注册表中的注册信息‖
    中的名称空间Microsoft.Wi
    RegistryKey类。在Registr
    :DeleteSubKey ( )方法、
    介绍一下在Visual C#中如
    有带类库,他对注册表的处理过
    n32中封装的二个类来实现的。
    yKey类中定义了三个方法来删除
    DeleteSubKeyTree ( )方法、De
    何正确使用这三个方法。
    程是通过调用.Net FrameWork SDK
    这二个类就是Registry类、
    注册表中的注册信息。他们分别是
    leteValue ( )方法。下面就具体


      一.如何用Visual C#中调用这三个方法:

      在介绍如何褂谜馊?br>--OpenSubKey ( )方法。在
    OpenSubKey( )方法有二种
    方法之前,还需要重新介绍一下
    上一文中已经介绍了,此方法是
    调用的方式:
    RegistryKey类中的一个方法
    打开指定的子键。其实


      I > .OpenSubKey ( string , subkey ) :这种调用方式是对于此子键只是进行读操作。

      II > .OpenSubKey ( string subkey , Boolean writable ):当对子键使用写操作的时候要用此种调用方法。如果在对子键使用了写操作,但仍然使用第一种调用方法,在程序运行的时候会产生一个错误信息。


      (1). DeleteSubKey ( )方法:                             
      此方法是删除一个指定
    ,则会产生一个错误信息。
    的子键,在使用此方法的时候,
    在程序中调用此方法有二种原型
    如果在此子键中还存在另外的子键
    ,为:
      I > . DeleteSubKey ( string , subkey ):这种调用方式就是直接删除指定的子键。


      II > . DeleteSubKey ( string subkey , Boolean info ):其中的"string"是要删除的子键的名称,"Boolean"参数的意思是:如果值为"True",则在程序调用的时候,删除的子键不存在,则产生一个错误信息;如果值为"False",则在程序调用的时候,删除的子键不存在,也不产生错误信息,程序依然正确运行。所以在具体的程序设计过程中,一故峭萍鍪褂玫诙值饔梅椒ā?


      (2). DeleteSubKeyTree ( )方法:                     
      此方法是彻底删除指定的子键目录,
    方法的破坏性是非常强的,所有在使用的
    ,为:
    即:删除该子键以及该子键以下的全部子键。由于此
    时候要非常主要。在程序中调用此方法的原型就一种


      DeleteSubKeyTree ( string subkey
    ):其中"subkey"就是要彻底删除的子键名称。

      (3). DeleteValue ( )方法:                               
      此方法是删除指定的键值。在程序中调用此方法的原型就一种,为:                 
      DeleteValue ( string
    value ):其中"value"就是要
    删除的键值的名称。
      在介绍完与删除注册表中注册信息有
    用法。
    关方法后,将通过一个程序来说明他们在程序中具体


      二. 程序设计和运行环境以及要准备的工作:

      I > .视窗系统2000服务器版


      II > ..Net FrameWork SDK Beta 2版


      III > .由于程序的功能是删除指定的主键、子键和键值,这就需要我们在注册表中先为设置好这些值的位置和名称。具体如下:

      在HKEY_LOCAL_MACHINE
    主键下面的"SOFTWARE"子键中建
    立如下子键和键值:
      在"SOFTWARE"子键下建
    "bbb"子键中建立名莆?cc
    在此子键中建立一个"fff"
    删除的子键是"bbb",要彻
    立"aaa"子键。在"aaa"子键下面
    c"的键值,键值的值为"ccc"。
    键值,键值的值为"fff"。程序
    底删除的子键是"ddd"。具体设
    建立"bbb"子键和"ddd"子键。在
    子"ddd"子键中建立子键"eee",并
    中要删除的键值是"ccc"键值,要
    定如下图所示:

      点击小图放大图01:为程序设定的注册表结构图                                 

      三. 程序设计的重要步骤:

      程序设计的主要步骤就
    就通过程序来咛逅得鳎?
    是如何删除键值、不包含任何子

    键的子键、包含子键的子键。下面

      (1).如何删除键值。在程序中要删
    语句。
    除键值是"ccc"。以下就是程序中删除此键值的具体

      RegistryKey hklm = Registry.Loca
    lMachine ;
      RegistryKey software
    = hklm.OpenSubKey ( "SOFTWA
    RE", true ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = so
    ftware.OpenSubKey ( "aaa", t
    rue ) ;
      //打开"aaa"子键                                                       
      RegistryKey no2 = no1.OpenSubKey
    ( "bbb" , true ) ;
      //打开"bbb"子键                                                       
      no2.DeleteValue( "ccc" ) ;                         
      //删除名称为"ccc"的键值                                               


      (2).如何删除不包含
    子键的具体程序代码:
    任何子键的子键。在程序要删除

    的子键是"bbb"。以下就是删除此


      RegistryKey hklm = R
    egistry.LocalMachine ;
      RegistryKey software = hklm.Open
    SubKey ( "SOFTWARE", true ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = so
    ftware.OpenSubKey ( "aaa", t
    rue ) ;
      //打开"aaa"子键                                                       
      no1.DeleteSubKey ( "bbb", false );         
      //删除名称为"bbb"的子键                                               

      (3).如何删除包含子键的子键。在
    键的具体程序代码:
    程序中要删除的此子键是"ddd"。以下就是删除此子


      RegistryKey hklm = R
    egistry.LocalMachine ;
      hklm.DeleteSubKey ( "aaa", false );       
      RegistryKey software
    = hklm.OpenSubKey ( "SOFTWA
    RE", true ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = software.OpenS
    ubKey ( "aaa", true ) ;
      //打开"aaa"子键                                                       
      no1.DeleteSubKeyTree ( "ddd" );               
      //删除名称为"ddd"的子键                                               

      四. 本文中的程序源代码( reg.cs )以及运行界面:

      reg.cs程序的主要功能
    并且通过按钮"读取注册表"
    的界面:
    就是删除注册表中的键值、不包
    ,以列表的显示方法来及时了解

    含子键的子键和包含子键的子键。
    删除的情况。下图就是程序运行后


      点击小图放大图02:本文中程序的运行界面                                     

      reg.cs程序源代码如下:                                                 
      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.Co
    ntainer 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.Componen
    tModel.Container ( ) ;
      button1 = new Button ( ) ;                         
      button2 = new Button ( ) ;                         
      button3 = new Button ( ) ;                         
      button4 = new Button ( ) ;                         
      listBox1 = new ListBox ( ) ;                     
      button1.Location = n
    ew System.Drawing.Point ( 16
    , 320 ) ;
      button1.Size = new S
    ystem.Drawing.Size ( 75 , 23
    ) ;
      button1.TabIndex = 0 ;                                 
      button1.Text = "读取注册表" ;                             
      button1.Click += new System.Even
    tHandler ( button1_Click ) ;

      button2.Location = new System.Dr
    awing.Point ( 116 , 320 ) ;
      button2.Size = new S
    ystem.Drawing.Size ( 75 , 23
    ) ;
      button2.TabIndex = 0 ;                                 
      button2.Text = "删除键值ccc" ;                         
      button2.Click += new
    System.EventHandler ( butto
    n2_Click ) ;

      button3.Location = n
    ew System.Drawing.Point ( 21
    6 , 320 ) ;
      button3.Size = new S
    ystem.Drawing.Size ( 75 , 23
    ) ;
      button3.TabIndex = 0 ;                                 
      button3.Text = "删除子键bbb" ;                         
      button3.Click += new
    System.EventHandler ( butto
    n3_Click ) ;

      button4.Location = new System.Dr
    awing.Point ( 316 , 320 ) ;
      button4.Size = new S
    ystem.Drawing.Size ( 75 , 23
    ) ;
      button4.TabIndex = 0 ;                                 
      button4.Text = "删除主键ddd" ;                         
      button4.Click += new
    System.EventHandler ( butto
    n4_Click ) ;

      listBox1.Location = new System.D
    rawing.Point ( 16 , 32 ) ;
      listBox1.Size = new
    System.Drawing.Size ( 496 ,
    264 ) ;
      listBox1.TabIndex = 1 ;                               

      this.Text = "用Visual C#来删除注
    册表中的主键、子键和键值!" ;
      this.AutoScaleBaseSi
    ze = new System.Drawing.Size
    ( 5 , 13 ) ;
      this.ClientSize = ne
    w 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 ( o
    bject sender , System.EventArgs e )
      {                                                                           
      listBox1.Items.Clear ( ) ;                         

      RegistryKey hklm = R
    egistry.LocalMachine ;
      RegistryKey software = hklm.Open
    SubKey ( "SOFTWARE" ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = software.OpenS
    ubKey ( "aaa" ) ;
      //打开"aaa"子键                                                       
      foreach ( string sit
    e in no1.GetSubKeyNames ( )
    )
      //开始遍历由子键名称组成的字符串数组                                       
      {                                                                           
      listBox1.Items.Add ( site ) ;                   
      //在列表中加入子键名称                                                     
      RegistryKey sitekey = no1.OpenSu
    bKey ( site ) ;
      //打开此子键                                                               
      foreach ( string sValName in sit
    ekey.GetValueNames ( ) )
      //开始遍历由指定子键拥有的键值名称组成的字符串数组                         
      {                                                                           
      listBox1.Items.Add ( " " + sValN

    ame + ": " + sitekey.GetValue ( sValName ) ) ;

      //在列表中加入键名称和对应的键值                                           
      }                                                                           
      }                                                                           
      }                                                                           
      protected void button2_Click ( o
    bject sender , System.EventArgs e )
      {                                                                           
      RegistryKey hklm = Registry.Loca
    lMachine ;
      RegistryKey software
    = hklm.OpenSubKey ( "SOFTWA
    RE", true ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = so
    ftware.OpenSubKey ( "aaa", t
    rue ) ;
      //打开"aaa"子键                                                       
      RegistryKey no2 = no1.OpenSubKey
    ( "bbb" , true ) ;
      //打开"bbb"子键                                                       
      no2.DeleteValue( "ccc" ) ;                         
      //删除名称为"ccc"的键值                                               
      }                                                                           
      protected void button3_Click ( o
    bject sender , System.EventArgs e )
      {                                                                           
      RegistryKey hklm = Registry.Loca
    lMachine ;
      RegistryKey software = hklm.Open
    SubKey ( "SOFTWARE", true ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = so
    ftware.OpenSubKey ( "aaa", t
    rue ) ;
      //打开"aaa"子键                                                       
      no1.DeleteSubKey ( "bbb", false );         
      //删除名称为"bbb"的子键                                               
      }                                                                           
      protected void button4_Click ( o
    bject sender , System.EventArgs e )
      {                                                                           
      RegistryKey hklm = Registry.Loca
    lMachine ;
      hklm.DeleteSubKey ( "aaa", false );       
      RegistryKey software
    = hklm.OpenSubKey ( "SOFTWA
    RE", true ) ;
      //打开"SOFTWARE"子键                                             
      RegistryKey no1 = software.OpenS
    ubKey ( "aaa", true ) ;
      //打开"aaa"子键                                                       
      no1.DeleteSubKeyTree ( "ddd" );               
      //删除名称为"ddd"的子键                                               
      }                                                                           
      public static void Main ( )                       
      {                                                                           
      Application.Run ( new Form1 ( ) ) ;       
      }                                                                           
      }                                                                           

      五. 总结:

      本文介绍Visual C#注
    信息是一项非常具有破坏性
    册表编程的一个重要内容,即:
    的操作,所以在操作之前一定要
    如何删除注册信息。由于删除注册
    注意对注册表的保护工作。

    上一页 下一页




    map