c# .Net操作注册表问题
By:Roy.LiuLast updated:2008-12-24
建了一个控制台程序,操作注册表测试,在创建的过程中,折腾够了,一切都是因为自己太贪心,一口想吃成个胖子,不一步一步的来做。
static public void WriteReg()
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Software = localMachine.OpenSubKey(@"SOFTWARE\LZS",true);
if (Software == null)
{
RegistryKey LZS = Software.CreateSubKey("LZS");
RegistryKey SOFT = LZS.CreateSubKey("author");
LZS.SetValue("SITE", "9893.COM");
SOFT.SetValue("AUTHOR", 9893);
}
Console.Read();
}
这样做有一个很明显的错误,就是如果在 SOFTWEAR下面没有 LZS 这个项,那么 software 就是 null,用 Software.CreateSubKey("LZS"); 自然就会报错“未将对象引用设置到对象的实例”。
所以解决的方法是分开来做。
static public void WriteReg()
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Software = localMachine.OpenSubKey(@"SOFTWARE",true);
if (Software != null)
{
RegistryKey LZS = Software.CreateSubKey("LZS");
RegistryKey SOFT = LZS.CreateSubKey("author");
LZS.SetValue("SITE", "9893.COM");
SOFT.SetValue("AUTHOR", 9893);
}
Console.Read();
}
这样自然就可以创建注册表键值了。
最后新建一个控制台程序,测试正确,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DelReg();
Console.Read();
}
static public void WriteReg()
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Software = localMachine.OpenSubKey(@"SOFTWARE",true);
if (Software != null)
{
RegistryKey LZS = Software.CreateSubKey("LZS");
RegistryKey SOFT = LZS.CreateSubKey("author");
LZS.SetValue("SITE", "9893.COM");
SOFT.SetValue("AUTHOR", 9893);
}
Console.Read();
}
static public void readReg()
{
RegistryKey readkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LZS");
if (readkey!=null)
{
string mysite = readkey.GetValue("SITE").ToString();
Console.WriteLine(mysite);
}
}
static public void SaveReg()
{
RegistryKey readkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LZS",true);
if (readkey != null)
{
readkey.SetValue("SITE","9893.COM网站");
}
}
static public void DelReg()
{
RegistryKey readkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LZS", true);
if (readkey != null)
{
readkey.DeleteValue("SITE");
}
}
}
}
static public void WriteReg()
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Software = localMachine.OpenSubKey(@"SOFTWARE\LZS",true);
if (Software == null)
{
RegistryKey LZS = Software.CreateSubKey("LZS");
RegistryKey SOFT = LZS.CreateSubKey("author");
LZS.SetValue("SITE", "9893.COM");
SOFT.SetValue("AUTHOR", 9893);
}
Console.Read();
}
这样做有一个很明显的错误,就是如果在 SOFTWEAR下面没有 LZS 这个项,那么 software 就是 null,用 Software.CreateSubKey("LZS"); 自然就会报错“未将对象引用设置到对象的实例”。
所以解决的方法是分开来做。
static public void WriteReg()
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Software = localMachine.OpenSubKey(@"SOFTWARE",true);
if (Software != null)
{
RegistryKey LZS = Software.CreateSubKey("LZS");
RegistryKey SOFT = LZS.CreateSubKey("author");
LZS.SetValue("SITE", "9893.COM");
SOFT.SetValue("AUTHOR", 9893);
}
Console.Read();
}
这样自然就可以创建注册表键值了。
最后新建一个控制台程序,测试正确,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DelReg();
Console.Read();
}
static public void WriteReg()
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey Software = localMachine.OpenSubKey(@"SOFTWARE",true);
if (Software != null)
{
RegistryKey LZS = Software.CreateSubKey("LZS");
RegistryKey SOFT = LZS.CreateSubKey("author");
LZS.SetValue("SITE", "9893.COM");
SOFT.SetValue("AUTHOR", 9893);
}
Console.Read();
}
static public void readReg()
{
RegistryKey readkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LZS");
if (readkey!=null)
{
string mysite = readkey.GetValue("SITE").ToString();
Console.WriteLine(mysite);
}
}
static public void SaveReg()
{
RegistryKey readkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LZS",true);
if (readkey != null)
{
readkey.SetValue("SITE","9893.COM网站");
}
}
static public void DelReg()
{
RegistryKey readkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LZS", true);
if (readkey != null)
{
readkey.DeleteValue("SITE");
}
}
}
}
From:一号门

COMMENTS