将数据写到注册表时,如果指定的键不存在,则会自动创建,这没有问题。但是读取数据时,如果指定的[项目!]不存在,可以使用默认值,但是如果指定的[键!]不存在,则会返回空引用,但这并不是我们所期望的啊,我们期望返回的是默认值。(键相当于文件夹,项目相当于文件名,数据相当于文件的内容)事实上,如果我们要把程序的配置写到注册表中,往往会创建一个新键,但是我们可以用SetValue直接写入数据,而不必用CreateSubKey来专门创建一个键,因为如果不存在该键,SetValue会自动创建但是我们读取数据时,可能并不存在相应的键,我们希望使用默认值,非常遗憾的是——只有当键存在时,才能使用(项目可以不存在)难道读取数据时必须判断指定的键是否存在或者自己创建一个吗?为什么不存在时,不总是返默认值?
{
private RegistryKey rootkey;
/// <summary>
/// 构造根键为RootKey的注册表操作类,缺省打开Current_User主键
/// </summary>
public ReadWriteReg(string RootKey)
{
switch (RootKey.ToUpper())
{
case "CLASSES_ROOT":
rootkey=Registry.ClassesRoot;
break;
case "CURRENT_USER":
rootkey=Registry.CurrentUser;
break;
case "LOCAL_MACHINE":
rootkey=Registry.LocalMachine;
break;
case "USERS":
rootkey=Registry.Users;
break;
case "CURRENT_CONFIG":
rootkey=Registry.CurrentConfig;
break;
case "DYN_DATA":
rootkey=Registry.DynData;
break;
case "PERFORMANCE_DATA":
rootkey=Registry.PerformanceData;
break;
default:
rootkey=Registry.CurrentUser;
break;
}
}
/// <summary>
/// 返回指定的keypath路径是否存在
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public bool IsExist(string keypath)
{
try
{
RegistryKey key=rootkey.OpenSubKey(keypath);
if (key==null)
{
return false;
}
else
{
return true;
}
}
catch
{
return false;
}
}
/// <summary>
/// 读取路径为keypath,键名为keyname的注册表键值,缺省返回def
/// </summary>
/// <param name="keypath"></param>
/// <param name="keyname"></param>
/// <param name="def"></param>
/// <returns></returns>
public string GetRegVal(string keypath,string keyname,string def)
{
try
{
RegistryKey key=rootkey.OpenSubKey(keypath);
return key.GetValue(keyname,(object)def).ToString();
}
catch
{
return def;
}
}
/// <summary>
/// 设置路径为keypath,键名为keyname的注册表键值为keyval
/// </summary>
/// <param name="keypath"></param>
/// <param name="keyname"></param>
/// <param name="keyval"></param>
public bool SetRegVal(string keypath,string keyname,string keyval)
{
try
{
RegistryKey key=rootkey.OpenSubKey(keypath,true);
key.SetValue(keyname,(object)keyval);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 创建路径为keypath的键
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public RegistryKey CreateRegKey(string keypath)
{
try
{
return rootkey.CreateSubKey(keypath);
}
catch
{
return null;
}
}
/// <summary>
/// 删除路径为keypath的子项
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public bool DelRegSubKey(string keypath)
{
try
{
rootkey.DeleteSubKey(keypath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 删除路径为keypath的子项及其附属子项
/// </summary>
/// <param name="keypath"></param>
/// <returns></returns>
public bool DelRegSubKeyTree(string keypath)
{
try
{
rootkey.DeleteSubKeyTree(keypath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 删除路径为keypath下键名为keyname的键值
/// </summary>
/// <param name="keypath"></param>
/// <param name="keyname"></param>
/// <returns></returns>
public bool DelRegKeyVal(string keypath,string keyname)
{
try
{
RegistryKey key=rootkey.OpenSubKey(keypath,true);
key.DeleteValue(keyname);
return true;
}
catch
{
return false;
}
}
}
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
RegistryKey readReg = software.OpenSubKey("YiShengSweater\\Database");
Production.Classes.EncryptDecrypt EDString = new Production.Classes.EncryptDecrypt(); if (readReg == null)
{
//创建数据库参数到注册表中
string password = EDString.StrEncrypt(""); //加密密码 RegistryKey mainKey = software.CreateSubKey("YiShengSweater");
RegistryKey db_host = mainKey.CreateSubKey("Database");
db_host.SetValue("ServerName", "127.0.0.1");
db_host.SetValue("DBName", "Woollen_Process");
db_host.SetValue("DBUser", "sa");
db_host.SetValue("DBPassword", password); txtServerName.Text = "127.0.0.1";
txtDBName.Text = "Woollen_Process";
txtDBUser.Text = "sa";
txtDBPassword.Text = "";
}
else
{
//读取注册表中的数据
txtServerName.Text = readReg.GetValue("ServerName").ToString().Trim();
txtDBName.Text = readReg.GetValue("DBName").ToString().Trim();
txtDBUser.Text = readReg.GetValue("DBUser").ToString().Trim();
txtDBPassword.Text = EDString.StrDecrypt(readReg.GetValue("DBPassword").ToString().Trim());
}//保存 private void btnSave_Click(object sender, EventArgs e)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey mainKey = software.CreateSubKey("YiShengSweater");
RegistryKey db_host = mainKey.CreateSubKey("Database"); string password = txtDBPassword.Text.Trim();
password = (new Production.Classes.EncryptDecrypt()).StrEncrypt(password);
db_host.SetValue("ServerName", txtServerName.Text.Trim());
db_host.SetValue("DBName", txtDBName.Text.Trim());
db_host.SetValue("DBUser", txtDBUser.Text.Trim());
db_host.SetValue("DBPassword", password);
MessageBox.Show("數據庫連接參數保存成功﹗", "系统提示﹕", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
using System.Diagnostics;
private void Access_Registry()
{
// 在HKEY_LOCAL_MACHINE\Software下建立一新键,起名为MCBInc
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// 增加一个子键
RegistryKey newkey = key.CreateSubKey("MCBInc");
// 设置此子键的值
newkey.SetValue("MCBInc", "NET Developer");
// 从注册表的其他地方获取数据
// 找出你的CPU
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
Object val = pRegKey.GetValue("VendorIdentifier");
Debug.WriteLine("The central processor of this machine is:"+ val);
// 删除键值
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true);
delKey.DeleteSubKey("MCBInc");
}
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
//Registry.CurrentUser.CreateSubKey("myprogram"); //注释掉该句后,第一次并不显示Hello World,只会显示一个Hello,既然如此,GetValue的默认值还有什么意义呢?SetValue也不用自动创建不存在的键了,因为微软逼得大家必须专门去创建新键
string s = (string)Registry.GetValue(@"HKEY_CURRENT_USER\myprogram", "message", "World");
Console.WriteLine("Hello " + s);
Console.Write("Please input:");
string t = Console.ReadLine();
Console.WriteLine("Save(y/n)?");
if (Console.Read() == 'y')
Registry.SetValue(@"HKEY_CURRENT_USER\myprogram", "message", t);
}
}
}