C#操作注册表及设置开机自动运行
来源:互联网 发布:网站怎么优化排名靠前 编辑:程序博客网 时间:2024/05/01 19:41
using Microsoft.Win32;
以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
1.读取指定名称的注册表的值
private string GetRegistData(string name)
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
registData = aimdir.GetValue(name).ToString();
return registData;
}
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
registData = aimdir.GetValue(name).ToString();
return registData;
}
以上是读取的注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下的XXX目录中名称为name的注册表值;
2.向注册表中写数据
private void WTRegedit(string name,string tovalue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.CreateSubKey("XXX");
aimdir.SetValue(name,tovalue);
}
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.CreateSubKey("XXX");
aimdir.SetValue(name,tovalue);
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项;
3.删除注册表中指定的注册表项
private void DeleteRegist(string name)
{
string[] aimnames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
aimnames = aimdir.GetSubKeyNames();
foreach(string aimKey in aimnames)
{
if(aimKey == name)
aimdir.DeleteSubKeyTree(name);
}
}
{
string[] aimnames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
aimnames = aimdir.GetSubKeyNames();
foreach(string aimKey in aimnames)
{
if(aimKey == name)
aimdir.DeleteSubKeyTree(name);
}
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中删除名称为name注册表项;
4.判断指定注册表项是否存在
private bool IsRegeditExit(string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
subkeyNames = aimdir.GetSubKeyNames();
foreach(string keyName in subkeyNames)
{
if(keyName == name)
{
_exit = true;
return _exit;
}
}
return _exit;
}
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
subkeyNames = aimdir.GetSubKeyNames();
foreach(string keyName in subkeyNames)
{
if(keyName == name)
{
_exit = true;
return _exit;
}
}
return _exit;
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;
==================================================================================
下面是设置开机自动运行的设置及操作:
#region 开机自动启动项设置及相关函数
//此处是一个菜单的单击事件
private void menuItem_Start_Click(object sender, EventArgs e)
{
//应该程序的路径
string keyValue = Application.ExecutablePath;
//如果菜单前面已经是选中状态,则删除开机自动运行,把菜单前面的钩去掉
if (this.menuItem_Start.CheckState == CheckState.Checked)
{
DeleteKey(keyName);
this.menuItem_Start.CheckState = CheckState.Unchecked;
return;
}
//设置开机自动运行的值,对应的路径(如C:Program FilesWinRARWinRAR.exe)
WriteKey(keyName, keyValue);
this.menuItem_Start.CheckState = CheckState.Checked;
}
//判断是否已经存在此键值,此处可以在Form_Load中来使用。
//如果存在,菜单[开机自动运行]前面可以打上对钩
//如果不存在,则不操作
private bool IsExistKey(string keyName)
{
bool _exist = false;
RegistryKey hklm = Registry.LocalMachine;
RegistryKey runs = hklm.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run", true);
//注意此处用的是GetValueNames()
string[] runsName = runs.GetValueNames();
foreach (string strName in runsName)
{
if (strName.ToUpper() == keyName.ToUpper())
{
_exist = true;
return _exist;
}
}
return _exist;
}
private bool WriteKey(string keyName, string keyValue)
{
RegistryKey hklm = Registry.LocalMachine;
//定义hklm指向注册表的LocalMachine,其中Software/Microsoft/Windows/CurrentVersion/Run就是关系到系统中随系统启动而启动的程序,通称启动项
RegistryKey run = hklm.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run");
try
{
//将我们的程序加进去
run.SetValue(keyName,keyValue);
//注意,一定要关闭,注册表应用。
hklm.Close();
return true;
}
catch //这是捕获异常的
{
return false;
}
}
//删除键值
private void DeleteKey(string keyName)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey runs = hklm.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run", true);
try
{
//注意此处用的是GetValueNames()
string[] runsName = runs.GetValueNames();
foreach (string strName in runsName)
{
if (strName.ToUpper() == keyName.ToUpper())
runs.DeleteValue(strName,false);
}
}
catch { }
}
#endregion
//此处是一个菜单的单击事件
private void menuItem_Start_Click(object sender, EventArgs e)
{
//应该程序的路径
string keyValue = Application.ExecutablePath;
//如果菜单前面已经是选中状态,则删除开机自动运行,把菜单前面的钩去掉
if (this.menuItem_Start.CheckState == CheckState.Checked)
{
DeleteKey(keyName);
this.menuItem_Start.CheckState = CheckState.Unchecked;
return;
}
//设置开机自动运行的值,对应的路径(如C:Program FilesWinRARWinRAR.exe)
WriteKey(keyName, keyValue);
this.menuItem_Start.CheckState = CheckState.Checked;
}
//判断是否已经存在此键值,此处可以在Form_Load中来使用。
//如果存在,菜单[开机自动运行]前面可以打上对钩
//如果不存在,则不操作
private bool IsExistKey(string keyName)
{
bool _exist = false;
RegistryKey hklm = Registry.LocalMachine;
RegistryKey runs = hklm.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run", true);
//注意此处用的是GetValueNames()
string[] runsName = runs.GetValueNames();
foreach (string strName in runsName)
{
if (strName.ToUpper() == keyName.ToUpper())
{
_exist = true;
return _exist;
}
}
return _exist;
}
private bool WriteKey(string keyName, string keyValue)
{
RegistryKey hklm = Registry.LocalMachine;
//定义hklm指向注册表的LocalMachine,其中Software/Microsoft/Windows/CurrentVersion/Run就是关系到系统中随系统启动而启动的程序,通称启动项
RegistryKey run = hklm.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run");
try
{
//将我们的程序加进去
run.SetValue(keyName,keyValue);
//注意,一定要关闭,注册表应用。
hklm.Close();
return true;
}
catch //这是捕获异常的
{
return false;
}
}
//删除键值
private void DeleteKey(string keyName)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey runs = hklm.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run", true);
try
{
//注意此处用的是GetValueNames()
string[] runsName = runs.GetValueNames();
foreach (string strName in runsName)
{
if (strName.ToUpper() == keyName.ToUpper())
runs.DeleteValue(strName,false);
}
}
catch { }
}
#endregion
- C#操作注册表及设置开机自动运行
- C#操作注册表及设置开机自动运行
- 操作注册表及设置开机自动运行
- c#设置软件开机自动运行,修改注册表
- c#设置软件开机自动运行,修改注册表
- 开机自动运行程序的注册表设置及写法
- C#通用类库--设置开机自运行禁用任务管理器注册表等操作
- C# winform利用注册表信息实现开机自动运行软件
- C# 设置应用程序开机自动运行
- C# 设置应用程序开机自动运行
- 设置开机自动运行
- C#中设置开机自动运行和关机
- C#中设置开机自动运行和关机
- C# WinFORM通过注册表实现开机运行
- 开机启动运行的注册表操作(MFC)
- linux开机自动运行设置
- linux开机自动运行设置
- VC 设置开机自动运行
- js控制图片显示的大小
- 如果监控Spring Bean生命周期
- 如何调试用于PPC设备的dll
- native2ascii的用法
- PPC U-Boot随读随写(1)
- C#操作注册表及设置开机自动运行
- Oracle 面试题目
- GNU gprof
- 冒泡排序与选择排序
- hg版本管理工具使用
- 一个自定义的Combox
- 一个自定义的Combox
- c#.net常用函数和方法集
- iis5.1 独立包 安装方法