软件开机启动设置

c#

        void 开机启动()
        {
            string 程序名 = Application.ExecutablePath,
            名称 = 程序名.Substring(程序名.LastIndexOf("\\") + 1);
            名称 = 名称.Remove(名称.LastIndexOf("."));
            if (!System.IO.File.Exists(程序名)) return;

            string 快捷文件 = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + 名称 + ".lnk";
            if (System.IO.File.Exists(快捷文件))
            {
                if (MessageBox.Show("确定要删除 [" + 名称 + "] 开机启动吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    System.IO.File.Delete(快捷文件);
            }
            else
            {
                WshShell 外壳 = new WshShell();
                IWshShortcut 快捷键 = (IWshShortcut)外壳.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + 名称 + ".lnk");
                快捷键.TargetPath = 程序名;
                快捷键.WorkingDirectory = System.Environment.CurrentDirectory;
                快捷键.WindowStyle = 1;
                快捷键.Description = 名称;
                快捷键.IconLocation = 程序名;
                快捷键.Save();
                MessageBox.Show("[" + 名称 + "] 开机启动,设置完成!");
            }
        }
        Microsoft.Win32.RegistryKey 注册项 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            //Microsoft.Win32.RegistryKey 注册项 = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
            //if (注册项 == null || !注册项.GetValueNames().Contains(名称))
            //    注册项.SetValue(名称, "\"" + 程序名 + "\" -autorun");/*设置开机启动WIN7/64带参数和带双引号*/
            //else
            //    if (注册项.ValueCount > 1)
            //        注册项.DeleteValue(名称);/*取消开机启动*/
            //注册项.Close();

            //string 程序路径 = System.Reflection.Assembly.GetExecutingAssembly().Location;  
            //RegistryKey src = Registry.LocalMachine.OpenSubKey("SOFTWARE", true).OpenSubKey("Microsoft", true).OpenSubKey("Windows", true).OpenSubKey("CurrentVersion", true).OpenSubKey("Run", true);



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值