开机自动启动注册,最终实现的效果如下:
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
{
}
}
}
}
原文地址:https://www.cnblogs.com/muzililong/p/10708079.html