往注册表中写入数据是大部分常见的Windows系统优化工具的根本方法,因此只要掌握了写入的方法且对注册表有足够的了解,读者完全可以用C#实现一个自己的系统优化工具。
实例如下:
using Microsoft.Win32;
class program
{
static void Main(string[ ] args)
{
SetRegistData("My Value");//写入数据
Console.WriteLine("写入注册表成功!");
Console.WriteLine("读取到的值为:");
Console.WriteLine(GetRegistData());//读取数据
SetRegistData(@ "C:\WINDOWS\system32");//恢复原始数据
}
static string GetRegistData()
{
string registData;//用于保存读取值的变量
//用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"
RegistryKey hkml=Registry.LocalMachine;
//获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键
RegistryKey software=hkml.OpenSubKey("SOFTWARE",true);
//获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键
RegistryKey microsoft=software.OpenSubKey("Microsoft",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键
RegistryKey help=windows.OpenSubKey("Help",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Help"下的"PINTLPAE.HLP"的值
registDatahelp.GetValue(@ "PINTLPAE.HLP").ToString();
Console.WriteLine("读取注册表成功");
return registData;
}
static void SetRegistData(string myValue)
{
//用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"
RegistryKey hkml=Registry.LocalMachine;
//获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键
RegistryKey software=hkml.OpenSubKey("SOFTWARE",true);
//获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键
RegistryKey microsoft=software.OpenSubKey("Microsoft",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键
RegistryKey help=windows.OpenSubKey("Help",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Help"下的"PINTLPAE.HLP"的值
registDatahelp.SetValue(@ "PINTLPAE.HLP",myValue).ToString();
}
//删除注册表的值
static void DelRegistData(string myValue)
{
//用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"
RegistryKey hkml=Registry.LocalMachine;
//获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键
RegistryKey software=hkml.OpenSubKey("SOFTWARE",true);
//获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键
RegistryKey microsoft=software.OpenSubKey("Microsoft",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键
RegistryKey help=windows.OpenSubKey("Help",true);
//删除子键“HKEY_LOCAL_MACHINE\Software\Microsoft\Windows|Help"下的"myValue"值
help.DeleteValue(myValue,false);
}
}
获取注册表中的所有值项
class program
{
static void Main(string[ ] args)
{
//读取所有该子键下的值项
string[ ] names=ListRegistData("myValue");
//列出所有值项
foreach(string name in names)
{
Console.WriteLine(name);
}
}
static string[ ] ListRegistData(string myValue)
{
RegistryKey help=EnterValue();
string[ ] names=help.GetValueNames();
return names;
}
private static RegistryKey EnterValue()
{
//用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"
RegistryKey hkml=Registry.LocalMachine;
//获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键
RegistryKey software=hkml.OpenSubKey("SOFTWARE",true);
//获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键
RegistryKey microsoft=software.OpenSubKey("Microsoft",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键
RegistryKey windows=microsoft.OpenSubKey("Windows",true);
//获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键
RegistryKey help=windows.OpenSubKey("Help",true);
return help;
}
}