锁定老帖子 主题:C# 操作注册表
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-19
名字空间Microsoft.Win32 1
![]() 2 ![]() 3 ![]() 4 ![]() 5 ![]() 6 ![]() 7 ![]() 8 ![]() 9 ![]() 10 ![]() 11 ![]() 12 ![]() 13 ![]() 14 ![]() 15 ![]() 16 ![]() 17 ![]() 18 ![]() 19 ![]() 20 ![]() 21 ![]() 22 ![]() 23 ![]() 24 ![]() 25 ![]() 26 ![]() 27 ![]() 28 ![]() 29 ![]() 30 ![]() 31 ![]() 32 ![]() 33 ![]() 34 ![]() 35 ![]() 36 ![]() 37 ![]() 38 ![]() 39 ![]() 40 ![]() 41 ![]() 42 ![]() 43 ![]() 44 ![]() 45 ![]() 46 ![]() 47 ![]() 48 ![]() 49 ![]() 50 ![]() 51 ![]() 52 ![]() 53 ![]() 54 ![]() 55 ![]() 56 ![]() 57 ![]()
注册表是视窗系统的一个核心的数据库,在这个数据库中存放中与系统相关的各种参数,这些参数直接控制中系统的启动、硬件的驱动程序安装信息以及在视窗系统上运行的各种应用程序的注册信息等。这就意味着,如果注册表因为某些原因受到了破坏,轻者是视窗系统启动过程出现异常,重者就有可能导致整个系统的完全瘫痪。所以正确的认识注册表,及时的备份注册表,对于视窗用户就显得相当重要。 Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 Registry.CurrentUser 对应于HKEY_CURRENT_USER主键 Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键 Registry.User 对应于 HKEY_USER主键 Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG主键 Registry.DynDa 对应于HKEY_DYN_DATA主键 Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA主键 (2).RegistryKey类:此类中主要封装了对视窗系统注册表的基本操作。在程序设计中,首先通过Registry类找到注册表中的基本主键,然后通过RegistryKey类,来找其下面的子键和处理具体的操作的。 OpenSubKey ( string name )方法主要是打开指定的子键。 GetSubKeyNames ( )方法是获得主键下面的所有子键的名称,它的返回值是一个字符串数组。 GetValueNames ( )方法是获得当前子键中的所有的键名称,它的返回值也是一个字符串数组。 GetValue ( string name )方法是指定键的键值。 程序中具体的使用语句如下: RegistryKey hklm = Registry.LocalMachine ; //打开"SYSTEM"子键 RegistryKey software = hklm.OpenSubKey ( "SYSTEM" ) ; //打开"001"子键 RegistryKey no1 = software.OpenSubKey ( "001" ) ; //打开"002"子键 RegistryKey no2 = no1.OpenSubKey ( "002" ) ; 其中listBox1是程序中定义了的列表名称。 foreach ( string site in no2.GetSubKeyNames ( ) ) //开始遍历由子键名称组成的字符串数组 { listBox1.Items.Add ( site ) ; //在列表中加入子键名称 RegistryKey sitekey = no2.OpenSubKey ( site ) ; //打开此子键 foreach ( string sValName in sitekey.GetValueNames ( ) ) //开始遍历由指定子键拥有的键值名称组成的字符串数组 { listBox1.Items.Add ( "" sValName ": " sitekey.GetValue ( sValName ) ) ; //在列表中加入键名称和对应的键值 } } 通过以上的论述,我们可以得到程序的源程序代码,具体如下: 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 ; public Form1 ( ) { InitializeComponent ( ) ; } //清除在程序中使用过的资源 public override void Dispose ( ) { base.Dispose ( ) ; components.Dispose ( ) ; } //初始化程序中使用到的组件 private void InitializeComponent ( ) { 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 ( 75 , 23 ) ; button1.TabIndex = 0 ; button1.Text = "读取注册表" ; button1.Click = new System.EventHandler( this.button1_Click ) ; listBox1.Location = new System.Drawing.Point ( 16 , 32 ) ; listBox1.Size = new System.Drawing.Size ( 496 , 264 ) ; listBox1.TabIndex = 1 ; this.Text = "读取主测表信息" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ; this.ClientSize = new System.Drawing.Size ( 528 , 357 ) ; this.Controls.Add( this.listBox1 ) ; this.Controls.Add ( this.button1 ) ; } // protected void button1_Click ( object sender , System.EventArgs e ) { listBox1.Items.Clear ( ) ; RegistryKey hklm = Registry.LocalMachine ; RegistryKey software = hklm.OpenSubKey ( "SYSTEM" ) ; //打开"SYSTEM"子键 RegistryKey no1 = software.OpenSubKey ( "001" ) ; //打开"001"子键 RegistryKey no2 = no1.OpenSubKey ( "002" ) ; //打开"002"子键 foreach ( string site in no2.GetSubKeyNames ( ) ) //开始遍历由子键名称组成的字符串数组 { listBox1.Items.Add ( site ) ; //在列表中加入子键名称 RegistryKey sitekey = no2.OpenSubKey ( site ) ; //打开此子键 foreach ( string sValName in sitekey.GetValueNames ( ) ) //开始遍历由指定子键拥有的键值名称组成的字符串数组 { listBox1.Items.Add ( "" sValName ": " sitekey.GetValue ( sValName ) ) ; //在列表中加入键名称和对应的键值 } } } // public static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } } 用C#读取注册表中的注册信息是通过名称空间Micorsoft.Win32中的二个类来实现的。在这二个类中还定义了对注册表信息的删除、修改和重命名的一些方法。这些方法比起本文介绍的读取方法、打开方法来说,更具有破坏性,但也更实用。由于注册表在视窗系统中的重要作用,所以在每一次对注册表进行操作之前,一定要备份,在操作的时候也要非常小心,因为一次的误操作都可能导致系统崩溃。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 1323 次