A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© q55964133 中级黑马   /  2014-5-23 12:43  /  1204 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

在别人网站看到的例子
觉得不错 论坛好像还没有 就发过来了

额。。。。。
以后有可能做软件 写可以写到注册表什么的 。。。或是注册码 通过注册表获取。。。等等。。
附上源码还有详细说明吧~~
在这里先谢谢 黑马蔡红微老师 上次给我加的黑马技术分。。

  1. 以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
  2. 1.读取指定名称的注册表的值
  3. private string GetRegistData(string name)
  4. {
  5.    string registData;
  6.    RegistryKey hkml = Registry.LocalMachine;
  7.    RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
  8.    RegistryKey aimdir = software.OpenSubKey("XXX",true);
  9.    registData = aimdir.GetValue(name).ToString();
  10.    return registData;
  11. }
  12. 以上是读取的注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下的XXX目录中名称为name的注册表值;

  13. 2.向注册表中写数据
  14. private void WTRegedit(string name,string tovalue)
  15. {
  16.    RegistryKey hklm = Registry.LocalMachine;
  17.    RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
  18.    RegistryKey aimdir = software.CreateSubKey("XXX");
  19.    aimdir.SetValue(name,tovalue);
  20. }
  21. 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项;

  22. 3.删除注册表中指定的注册表项
  23. private void DeleteRegist(string name)
  24. {
  25.    string[] aimnames;
  26.    RegistryKey hkml = Registry.LocalMachine;
  27.    RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
  28.    RegistryKey aimdir = software.OpenSubKey("XXX",true);
  29.    aimnames = aimdir.GetSubKeyNames();
  30.    foreach(string aimKey in aimnames)
  31.    {
  32.      if(aimKey == name)
  33.      aimdir.DeleteSubKeyTree(name);
  34.    }
  35. }
  36. 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中删除名称为name注册表项;

  37. 4.判断指定注册表项是否存在
  38. private bool IsRegeditExit(string name)
  39. {
  40.    bool _exit = false;
  41.    string[] subkeyNames;
  42.    RegistryKey hkml = Registry.LocalMachine;
  43.    RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
  44.    RegistryKey aimdir = software.OpenSubKey("XXX",true);
  45.    subkeyNames = aimdir.GetSubKeyNames();
  46.    foreach(string keyName in subkeyNames)
  47.    {
  48.      if(keyName == name)
  49.      {
  50.        _exit = true;
  51.        return _exit;
  52.      }
  53.    }
  54.    return _exit;
  55. }
  56. 以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;

  57. C#就可以十分方便、简洁的开发出操作注册表的程序。单击"开始/运行",在"打开"的后面填入"regedit"。就可以看到注册表的数据结构了。"主键"是有层次结构的。主键的下一级主键称为该主键的"子键"。每一个主键可以对拥有多个子键。右边的这些值就是所谓的键值了。每一个主键或者子键都可以拥有多个键值。注册表是一个庞大的数据库,在其中每一个主键,每一个键值都赋予了不同的功能。
  58. C#如何读取注册表中的主键和键值:在.Net FrameWork SDK Beta 2版中,有一个Microsoft.Win32的名称空间,在此名称空间中提供了二个用于注册表操作的类:Registry类、RegistryKey类。这二个类都是封闭类,不可以继承。这二个类定义了许多关于注册表的方法和属性,通过调用这二个类,在Visual C#中就可以比较轻松的处理关于注册表的各种操作了。
  59. (1).Registry类:此类主要封装了七个公有的静态域,而这些静态域分别代表这视窗注册表中的七个基本的主键,具体如下所示:
  60. Registry.ClassesRoot     对应于HKEY_CLASSES_ROOT主键
  61. Registry.CurrentUser     对应于HKEY_CURRENT_USER主键
  62. Registry.LocalMachine     对应于 HKEY_LOCAL_MACHINE主键
  63. Registry.User             对应于 HKEY_USER主键
  64. Registry.CurrentConfig   对应于HEKY_CURRENT_CONFIG主键
  65. Registry.DynDa           对应于HKEY_DYN_DATA主键
  66. Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA主键
  67. (2).RegistryKey类:此类中主要封装了对视窗系统注册表的基本操作。在程序设计中,首先通过Registry类找到注册表中的基本主键,然后通过RegistryKey类,来找其下面的子键和处理具体的操作的。
  68.      下面通过一个读取注册表信息例子来具体说明这二个来的用法。
  69. 程序设计和运行的环境:Windows 2000服务器版,.Net FrameWork SDK Beta 2版。
  70. 在运行程序前的一些必要的处理工作。在程序设计时,主要功能是读取已经存在的主键键值,用户可以新建若干个主键和对应的键值。
  71.      程序的主要功能是读取指定主键下面的所有子键和子键拥有的键值,并以列表的形式按层次显示出来。程序设计过程中的重要步骤以及应该注意的一些问题:
  72.      程序中读取主键、子键和键值所使用到的方法: 程序中为了读取指定主键下面的子键和子键中拥有的键值,主要使用了RegistryKey类中的四个方法:OpenSubKey,GetSubKeyNames,GetValueNames,GetValue。具体的用法和意思如下:
  73. OpenSubKey ( string name )方法主要是打开指定的子键。
  74. GetSubKeyNames ( )方法是获得主键下面的所有子键的名称,它的返回值是一个字符串数组。
  75. GetValueNames ( )方法是获得当前子键中的所有的键名称,它的返回值也是一个字符串数组。
  76. GetValue ( string name )方法是指定键的键值。
  77. 程序中具体的使用语句如下:
  78. RegistryKey hklm = Registry.LocalMachine ;
  79. //打开"SYSTEM"子键
  80. RegistryKey software = hklm.OpenSubKey ( "SYSTEM" ) ;
  81. //打开"001"子键
  82. RegistryKey no1 = software.OpenSubKey ( "001" ) ;
  83. //打开"002"子键
  84. RegistryKey no2 = no1.OpenSubKey ( "002" ) ;
  85. 其中listBox1是程序中定义了的列表名称。
  86. foreach ( string site in no2.GetSubKeyNames ( ) )
  87. //开始遍历由子键名称组成的字符串数组
  88. {
  89. listBox1.Items.Add ( site ) ;
  90. //在列表中加入子键名称
  91. RegistryKey sitekey = no2.OpenSubKey ( site ) ;
  92. //打开此子键
  93. foreach ( string sValName in sitekey.GetValueNames ( ) )
  94. //开始遍历由指定子键拥有的键值名称组成的字符串数组
  95. {
  96.    listBox1.Items.Add ( ""   sValName   ": "   sitekey.GetValue ( sValName ) ) ;
  97.    //在列表中加入键名称和对应的键值
  98. }
  99. }
  100. 通过以上的论述,我们可以得到程序的源程序代码,具体如下:
  101. using System ;
  102. using System.Drawing ;
  103. using System.Collections ;
  104. using System.ComponentModel ;
  105. using System.Windows.Forms ;
  106. using System.Data ;
  107. using Microsoft.Win32 ;
  108. public class Form1 : Form
  109. {
  110. private System.ComponentModel.Container components ;
  111. private ListBox listBox1 ;
  112. private Button button1 ;
  113. public Form1 ( )
  114. {
  115.    InitializeComponent ( ) ;
  116. }
  117. //清除在程序中使用过的资源
  118. public override void Dispose ( )
  119. {
  120.    base.Dispose ( ) ;
  121.    components.Dispose ( ) ;
  122. }
  123. //初始化程序中使用到的组件
  124. private void InitializeComponent ( )
  125. {
  126.    this.components = new System.ComponentModel.Container ( ) ;
  127.    this.button1 = new Button ( ) ;
  128.    this.listBox1 = new ListBox ( ) ;
  129.    button1.Location = new System.Drawing.Point ( 16 , 320 ) ;
  130.    button1.Size = new System.Drawing.Size ( 75 , 23 ) ;
  131.    button1.TabIndex = 0 ;
  132.    button1.Text = "读取注册表" ;
  133.    button1.Click   = new System.EventHandler( this.button1_Click ) ;
  134.    listBox1.Location = new System.Drawing.Point ( 16 , 32 ) ;
  135.    listBox1.Size = new System.Drawing.Size ( 496 , 264 ) ;
  136.    listBox1.TabIndex = 1 ;
  137.    this.Text = "读取主测表信息" ;
  138.    this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
  139.    this.ClientSize = new System.Drawing.Size ( 528 , 357 ) ;
  140.    this.Controls.Add( this.listBox1 ) ;
  141.    this.Controls.Add ( this.button1 ) ;
  142. }
  143. //
  144. protected void button1_Click ( object sender , System.EventArgs e )
  145. {
  146.    listBox1.Items.Clear ( ) ;
  147.    RegistryKey hklm = Registry.LocalMachine ;
  148.    RegistryKey software = hklm.OpenSubKey ( "SYSTEM" ) ;
  149.    //打开"SYSTEM"子键
  150.    RegistryKey no1 = software.OpenSubKey ( "001" ) ;
  151.    //打开"001"子键
  152.    RegistryKey no2 = no1.OpenSubKey ( "002" ) ;
  153.    //打开"002"子键
  154.    foreach ( string site in no2.GetSubKeyNames ( ) )
  155.    //开始遍历由子键名称组成的字符串数组
  156.    {
  157.    listBox1.Items.Add ( site ) ;
  158.    //在列表中加入子键名称
  159.    RegistryKey sitekey = no2.OpenSubKey ( site ) ;
  160.    //打开此子键
  161.    foreach ( string sValName in sitekey.GetValueNames ( ) )
  162.    //开始遍历由指定子键拥有的键值名称组成的字符串数组
  163.    {
  164.      listBox1.Items.Add ( ""   sValName   ": "   sitekey.GetValue ( sValName ) ) ;
  165.      //在列表中加入键名称和对应的键值
  166.    }
  167.    }
  168. }
  169. //
  170. public static void Main ( )
  171. {
  172.    Application.Run ( new Form1 ( ) ) ;
  173. }
  174. }
  175. 用C#读取注册表中的注册信息是通过名称空间Micorsoft.Win32中的二个类来实现的。在这二个类中还定义了对注册表信息的删除、修改和重命名的一些方法。这些方法比起本文介绍的读取方法、打开方法来说,更具有破坏性,但也更实用。由于注册表在视窗系统中的重要作用,所以在每一次对注册表进行操作之前,一定要备份,在操作的时候也要非常小心,因为一次的误操作都可能导致系统崩溃。

复制代码



3 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报

你已经毕业了吧?
回复 使用道具 举报
方杰斌 发表于 2014-5-23 15:21
你已经毕业了吧?

你猜呢!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马