创建注册表读写程序(二)
2014-11-06 17:39
246 查看
往注册表中写入数据是大部分常见的Windows系统优化工具的根本方法,因此只要掌握了写入的方法且对注册表有足够的了解,读者完全可以用C#实现一个自己的系统优化工具。
实例如下:
using Mic
a8ae
rosoft.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;
}
}
实例如下:
using Mic
a8ae
rosoft.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;
}
}
相关文章推荐
- 创建注册表读写程序(一)
- 程序设计中的读写注册表的编程介绍
- 服务程序C#编写系统服务,注册表读写问题
- 创建一个用于在程序目录中读写INI文件的类
- VB-读写注册表 程序
- DELPHI注册表读写,含创建子键
- QT执行外部程序,读写注册表
- C# 32位/64位系统下程序读写注册表之差异
- QSettings Windows读写注册表小测试程序
- 创建INI文件读写程序
- VC读写注册表实现程序自启动的实现源代码
- .Net中如何限制执行程序只能运行一次和如何读写注册表
- MFC程序可以这样读写注册表
- C# 32位/64位程序读写注册表之差异
- 如何在Windows服务程序中读写HKEY_CURRENT_USER注册表
- 读写注册表 registrykey 创建删除
- C#操作注册表 创建快速启动程序
- 服务程序C#编写系统服务,注册表读写问题
- VC读写注册表实现程序自启动的实现源代码
- 利用C#操作注册表 创建快速启动程序