程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用Visual C#來創建修改注冊信息(2)

用Visual C#來創建修改注冊信息(2)

編輯:關於C語言

四.本文中源程序代碼( reg.cs )

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.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 ( )
{
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 ( 90 , 23 ) ;
button1.TabIndex = 0 ;
button1.Text = "讀取注冊表" ;

button1.Click += new System.EventHandler ( this.button1_Click ) ;

this.button2 = new Button ( ) ;
button2.Location = new System.Drawing.Point ( 116 , 320 ) ;
button2.Size = new System.Drawing.Size ( 90 , 23 ) ;
button2.TabIndex = 1 ;
button2.Text = "創建子鍵" ;
button2.Click += new System.EventHandler ( this.button2_Click ) ;

this.button3 = new Button ( ) ;
button3.Location = new System.Drawing.Point ( 216 , 320 ) ;
button3.Size = new System.Drawing.Size ( 90 , 23 ) ;
button3.TabIndex = 2 ;
button3.Text = "創建主鍵" ;
button3.Click += new System.EventHandler ( this.button3_Click ) ;

this.button4 = new Button ( ) ;
button4.Location = new System.Drawing.Point ( 316 , 320 ) ;
button4.Size = new System.Drawing.Size ( 90 , 23 ) ;
button4.TabIndex = 3 ;
button4.Text = "重命名鍵值" ;
button4.Click += new System.EventHandler ( this.button4_Click ) ;

listBox1.Location = new System.Drawing.Point ( 16 , 32 ) ;
listBox1.Size = new System.Drawing.Size ( 496 , 264 ) ;
listBox1.TabIndex = 4 ;
this.Text = "用Visual C#來創建和修改注冊表中的注冊信息!" ;
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 ) ;
this.Controls.Add ( this.button2 ) ;
this.Controls.Add ( this.button3 ) ;
this.Controls.Add ( this.button4 ) ;
}
//以列表形式顯示"HARDWARE"下面一層的子鍵和鍵值
protected void button1_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE" ) ;
//打開"SYSTEM"子鍵
foreach ( string site in software.GetSubKeyNames ( ) )
//開始遍歷由子鍵名稱組成的字符串數組
{
listBox1.Items.Add ( site ) ;
//在列表中加入子鍵名稱
RegistryKey sitekey = software.OpenSubKey ( site ) ;
//打開此子鍵
foreach ( string sValName in sitekey.GetValueNames ( ) )
//開始遍歷由指定子鍵擁有的鍵值名稱組成的字符串數組
{
listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ;
//在列表中加入鍵名稱和對應的鍵值
}
}
}
//創建子鍵和鍵值
protected void button2_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey ddd = software.CreateSubKey ( "ddd" ) ;
ddd.SetValue ( "www" , "1234" );
}
//創建一個主鍵並創建一個鍵值
protected void button3_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey main1 = software.CreateSubKey ( "main" ) ;
RegistryKey ddd = main1.CreateSubKey ( "sub" ) ;
ddd.SetValue ( "value" , "1234" ) ;
}
//重命名一個存在的鍵值
protected void button4_Click ( object sender , System.EventArgs e )
{
listBox1.Items.Clear ( ) ;
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey software = hklm.OpenSubKey ( "HARDWARE", true ) ;
RegistryKey dddw = software.OpenSubKey ( "aaa" , true ) ;
dddw.SetValue ( "bbb" , "abcd" ) ;
}
public static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved