码迷,mamicode.com
首页 > 其他好文 > 详细

开机自动启动注册

时间:2019-04-15 00:15:48      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:microsoft   cal   msconfig   tostring   tar   key   lse   thread   console   

开机自动启动注册,最终实现的效果如下:

1.电脑运行msconfig可以看到,我的电脑由于是win10的,win7可以在启动兰看到对应电脑开机需要启动的那些程序:

技术图片

 

控制台程序如下:

技术图片

程序如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;


using Microsoft.Win32;

namespace start
{
class Program
{
static void Main(string[] args)
{

RunWhenStart(true);

Console.WriteLine("注册下开机启动");

Console.ReadKey();

}


static void RunWhenStart(bool started)
{
RegistryKey key = null;
try
{
key = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Run", true);
}
catch
{
return;
}

if (started == true)
{
try
{
if (key.GetValue(Application.ProductName) == null || key.GetValue(Application.ProductName).ToString().ToLower() != Application.ExecutablePath.ToLower())
key.SetValue(Application.ProductName, Application.ExecutablePath);
}
catch
{
CreateRunRegisterKey();
}
}
else
{
try
{
key.DeleteValue(Application.ProductName);
}
catch
{
}
}
key.Close();
}

private static void CreateRunRegisterKey()
{
try
{
Registry.LocalMachine.OpenSubKey("SOFTWARE").CreateSubKey("Microsoft").CreateSubKey("Windows").CreateSubKey("CurrentVersion").CreateSubKey("Run");
}
catch
{

}
}


}
}

 

开机自动启动注册

标签:microsoft   cal   msconfig   tostring   tar   key   lse   thread   console   

原文地址:https://www.cnblogs.com/muzililong/p/10708079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!