.net 怎么修改注册表中的值?? 别人对我说可以用这两个类:Microsoft.Win32.RegistryMicrosoft.Win32.RegustrtKey但这两个好像不能用,其中一个还是一个尚在构建中的大家有没有其它办法?最好能给一个小例子,谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 ( ) ) ; } } 我照你的方法添加一个键值,明明用的是Registry.CurrentUser,但怎么会跑到HKEY_USER里面操作呢?应该是:HKEY_CURRENT_USER呀! RegistryKey ObjReg = Registry.CurrentUser; RegistryKey ObjReg1 = ObjReg.OpenSubKey("Software\\Microsoft",true); RegistryKey NewKey = ObjReg1.CreateSubKey("sxdReg"); NewKey.SetValue("sxdReg","sxdReg"); Response.Write(ObjReg1.GetValue("User Agent")); ObjReg1.Close(); ObjReg.Close(); 具体看这里:http://community.csdn.net/Expert/topic/4263/4263262.xml?temp=.8397791 如何用VS2008制作Web安装程序,请指教。 关于C# Winform的开发 请问关于自定义验证控件的问题 如何在C#项目中使用java接口 怎样将回车符保存到数据库中 晕!有谁了解MDAC,ODBC,ADO之间的关系?多谢! 用.net2008做开发工具,大家推荐一个比较简单实用的ajax框架 asp.net中如何通过 WmiMonitorID查询显示器序列号? 为什么INSERT INTO 语句的语法错误? gridview多项选择删除后怎么刷新本页面并提示成功? 未将对象引用设置到对象的实例 关于DataGrid背景颜色的问题!在线等呀!!!!!!
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 ( ) ) ;
}
}
RegistryKey ObjReg1 = ObjReg.OpenSubKey("Software\\Microsoft",true);
RegistryKey NewKey = ObjReg1.CreateSubKey("sxdReg");
NewKey.SetValue("sxdReg","sxdReg"); Response.Write(ObjReg1.GetValue("User Agent"));
ObjReg1.Close();
ObjReg.Close();