C#[添加/消除]自启动项源代码

 
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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值