C# 开机自启动

using Microsoft.Win32;//添加命名空间

原文地址:http://wenda.so.com/q/1375246108065386

 /// <summary>
        /// 设置开机自启动__写入注册表
        /// </summary>
        /// <returns></returns>
        public static bool SetSelfStarting()
        {
            try
            {
                string exeDir = Application.ExecutablePath;//要启动的程序绝对路径
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey softWare = rk.OpenSubKey("SOFTWARE");
                RegistryKey microsoft = softWare.OpenSubKey("Microsoft");
                RegistryKey windows = microsoft.OpenSubKey("Windows");
                RegistryKey current = windows.OpenSubKey("CurrentVersion");
                RegistryKey run = current.OpenSubKey(@"Run", true);//这里必须加true就是得到写入权限 
                run.SetValue("自己起个名字", exeDir);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }        
        /// <summary>
        /// 取消开机自启__删除注册表
        /// </summary>
        /// <returns></returns>
        public static bool CancelSelfStarting()
        {
            try
            {
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey softWare = rk.OpenSubKey("Software");
                RegistryKey microsoft = softWare.OpenSubKey("Microsoft");
                RegistryKey windows = microsoft.OpenSubKey("Windows");
                RegistryKey current = windows.OpenSubKey("CurrentVersion");
                RegistryKey run = current.OpenSubKey(@"Run", true);
                run.DeleteValue("自己起的名字");//删除myPro的值
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


原文地址:http://www.cnblogs.com/steed-zgf/archive/2012/04/26/2471652.html

【设置开机自启动】

 

复制代码
using Microsoft.Win32;//添加命名空间

public static bool SetAutoRun(string keyName,string filePath)

        {

            try

            {

                RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);

                runKey.SetValue(keyName,filePath);

                runKey.Close();

            }

            catch

            {

                return false;

            }

            return true;

        }
复制代码

 

【取消开机自启动】

 

复制代码
using Microsoft.Win32;//添加命名空间

public static bool DeleteAutoRun(string keyName,string filePath)

        {

            try

            {

                RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);

                runKey.DeleteValue(keyName,filePath);

                runKey.Close();

            }

            catch

            {

                return false;

            }

            return true;

        }
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值