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