当前位置:编程学习 > C#/ASP.NET >>

修改注册表某个键值出错

本人刚接触C# 
修改注册表某个键值出错:  System.NullReferenceException: 未将对象引用设置到对象的实例
请达人解答~谢了
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;

namespace 修改注册表
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button 修改;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.修改 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// 修改
// 
this.修改.Location = new System.Drawing.Point(152, 72);
this.修改.Name = "修改";
this.修改.TabIndex = 0;
this.修改.Text = "修改";
this.修改.Click += new System.EventHandler(this.修改_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.修改);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}

private void 修改_Click(object sender, System.EventArgs e)
{
RegistryKey key = Registry.CurrentUser;
RegistryKey software = key.OpenSubKey("aaa",true); //该项必须已存在
software.SetValue("aaa", "ccc");

MessageBox.Show ("注册成功");
}
}
}
--------------------编程问答--------------------
引用楼主 zengsea 的帖子:
本人刚接触C# 
修改注册表某个键值出错:  System.NullReferenceException: 未将对象引用设置到对象的实例 
请达人解答~谢了 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using Microsoft.Win32; 

namespace 修改注册表 

/// <summary> 
/// Form1 的摘要说明。 
/// </summary> 
public class For…



RegistryKey key = Registry.CurrentUser; 
RegistryKey software = key.OpenSubKey("aaa",true); //该项必须已存在 
software.SetValue("aaa", "ccc"); 



key.OpenSubKey("aaa",true);  
OpenSubKey 必须是已存在的键值吧。

--------------------编程问答--------------------
引用 1 楼 arserangel 的回复:
引用楼主 zengsea 的帖子:
本人刚接触C# 
修改注册表某个键值出错:  System.NullReferenceException: 未将对象引用设置到对象的实例 
请达人解答~谢了 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using Microsoft.Win32; 

namespace 修改注册表 

/// <summary> 
/// Form1 的摘要说明。 
/// </s…


aaa 是我刚刚新建的 --------------------编程问答-------------------- 每人帮忙解答下吗?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,