1、创建一个winfrom工程
button1(设置开机自启)
button2(取消开机自启)
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//没加try catch 去掉注释就能使用,但不能捕获异常
//MessageBox.Show("设置开机自启动,需要修改注册表", "提示");
//string path = Application.ExecutablePath;
//RegistryKey rk = Registry.CurrentUser; 添加到 当前登陆用户的 注册表启动项
//RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
//rk2.SetValue("1111", path);
//rk2.Close();
//rk.Close();
MessageBox.Show("设置开机自启动,需要修改注册表", "提示");
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser; 添加到 当前登陆用户的 注册表启动项
try
{
// //SetValue:存储值的名称
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("1111", path);
MessageBox.Show("添加成功");
rk2.Close();
rk.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
//没加try catch 去掉注释就能使用,但不能捕获异常
//MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
//string path = Application.ExecutablePath;
//RegistryKey rk = Registry.CurrentUser;
//RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
//rk2.DeleteValue("1111", false);
//rk2.Close();
//rk.Close();
取消开机自启动
MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
try
{
SetValue:存储值的名称
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("1111", false);
MessageBox.Show("取消成功");
rk2.Close();
rk.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
当前登陆用户的 注册表启动项
try
{
// //SetValue:存储值的名称
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("1111", path);
MessageBox.Show("添加成功");
rk2.Close();
rk.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
//没加try catch 去掉注释就能使用,但不能捕获异常
//MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
//string path = Application.ExecutablePath;
//RegistryKey rk = Registry.CurrentUser;
//RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
//rk2.DeleteValue("1111", false);
//rk2.Close();
//rk.Close();
取消开机自启动
MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
try
{
SetValue:存储值的名称
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("1111", false);
MessageBox.Show("取消成功");
rk2.Close();
rk.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}