using
Microsoft.Win32;
// 检查一个自启动键是否存在.
private bool IsValueExist( string KeyValue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey run = hklm.CreateSubKey( @" SOFTWAREMicrosoftWindowsCurrentVersionRun " );
string ValueStr1 = null ;
try
{
ValueStr1 = ( string )run.GetValue(KeyValue);
hklm.Close();
}
catch (Exception Er1)
{
MessageBox.Show(Er1.Message.ToString(), " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (ValueStr1 == null )
return false ;
else
return true ;
}
// 添加注册表自启动键
private void SetAutoValue( string KeyValue)
{
// 检查键值是否已经存在,不会重复注册
bool RT = this .IsValueExist(KeyValue);
if (RT) {
// MessageBox.Show( KeyValue + "已经存在,不再建立");
return ;
}
RegistryKey hklm = Registry.LocalMachine;
RegistryKey run = hklm.CreateSubKey( @" SOFTWAREMicrosoftWindowsCurrentVersionRun " );
try
{
run.SetValue(KeyValue, Application.ExecutablePath); // 获得路径和文件名
MessageBox.Show( " 已经成功设置为自启动!! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch (Exception my)
{
MessageBox.Show(my.Message.ToString(), " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 删除指定注册表自启动键
private void DelAutoValue( string KeyValue)
{
// 检查键值是否已经存在,如果已不存在则不操作了
bool RT = this .IsValueExist(KeyValue);
if ( ! RT)
{
// MessageBox.Show(KeyValue + " 已不存在,无需操作");
return ;
}
RegistryKey hklm = Registry.LocalMachine;
RegistryKey run = hklm.CreateSubKey( @" SOFTWAREMicrosoftWindowsCurrentVersionRun " );
try
{
// run.SetValue(KeyValue, Application.ExecutablePath); // 获得路径和文件名
run.DeleteValue(KeyValue);
MessageBox.Show( " 自启动已经成功取消!! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch (Exception my)
{
MessageBox.Show(my.Message.ToString(), " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 主调函数
private void AutoStartSwitch( string KeyNameValue, bool IStart)
{
if (IStart)
this .SetAutoValue(KeyNameValue);
else
this .DelAutoValue(KeyNameValue);
return ;
}
/* 都做好以后使用起来就非常方便了:
* AutoStartSwitch("MyClock", true);
* 可以跟一个二态的复选框,或者菜单用在一起
* 很轻易的添加或消除自启动的注册项
* 只在操作成功之后,或者出现异常时,才有提示出现 */
引用: http: // blog.zjol.com.cn/tb.asp?id=138670&TBcode=200708211222130Mdq5DCw1A
// 检查一个自启动键是否存在.
private bool IsValueExist( string KeyValue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey run = hklm.CreateSubKey( @" SOFTWAREMicrosoftWindowsCurrentVersionRun " );
string ValueStr1 = null ;
try
{
ValueStr1 = ( string )run.GetValue(KeyValue);
hklm.Close();
}
catch (Exception Er1)
{
MessageBox.Show(Er1.Message.ToString(), " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (ValueStr1 == null )
return false ;
else
return true ;
}
// 添加注册表自启动键
private void SetAutoValue( string KeyValue)
{
// 检查键值是否已经存在,不会重复注册
bool RT = this .IsValueExist(KeyValue);
if (RT) {
// MessageBox.Show( KeyValue + "已经存在,不再建立");
return ;
}
RegistryKey hklm = Registry.LocalMachine;
RegistryKey run = hklm.CreateSubKey( @" SOFTWAREMicrosoftWindowsCurrentVersionRun " );
try
{
run.SetValue(KeyValue, Application.ExecutablePath); // 获得路径和文件名
MessageBox.Show( " 已经成功设置为自启动!! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch (Exception my)
{
MessageBox.Show(my.Message.ToString(), " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 删除指定注册表自启动键
private void DelAutoValue( string KeyValue)
{
// 检查键值是否已经存在,如果已不存在则不操作了
bool RT = this .IsValueExist(KeyValue);
if ( ! RT)
{
// MessageBox.Show(KeyValue + " 已不存在,无需操作");
return ;
}
RegistryKey hklm = Registry.LocalMachine;
RegistryKey run = hklm.CreateSubKey( @" SOFTWAREMicrosoftWindowsCurrentVersionRun " );
try
{
// run.SetValue(KeyValue, Application.ExecutablePath); // 获得路径和文件名
run.DeleteValue(KeyValue);
MessageBox.Show( " 自启动已经成功取消!! " , " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch (Exception my)
{
MessageBox.Show(my.Message.ToString(), " 提示 " , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 主调函数
private void AutoStartSwitch( string KeyNameValue, bool IStart)
{
if (IStart)
this .SetAutoValue(KeyNameValue);
else
this .DelAutoValue(KeyNameValue);
return ;
}
/* 都做好以后使用起来就非常方便了:
* AutoStartSwitch("MyClock", true);
* 可以跟一个二态的复选框,或者菜单用在一起
* 很轻易的添加或消除自启动的注册项
* 只在操作成功之后,或者出现异常时,才有提示出现 */
引用: http: // blog.zjol.com.cn/tb.asp?id=138670&TBcode=200708211222130Mdq5DCw1A