C#对注册表的启动项操作

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32; //操作注册表要用的名称空间
namespace regrun //自己命名的一个新的名称空间
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form //定义一个新类Form1,继承自System.Windows.Forms.Form
{
   private System.Windows.Forms.Button button1;//声明button按钮,其定义在后面会有详细的描述
   private System.Windows.Forms.Button button2;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;
   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }
   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }
   #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(98, 112);//button1的位置
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(96, 24);//button1的大小
    this.button1.TabIndex = 0;
    this.button1.Text = "添加启动项";//button1上显示的文字
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(98, 48);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(96, 24);
    this.button2.TabIndex = 1;
    this.button2.Text = "移除启动项";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
   }
   #endregion
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }
   private void button1_Click(object sender, System.EventArgs e) //button1按下后,会执行的方法
   {
   RegistryKey hklm=Registry.LocalMachine;
    RegistryKey run=hklm.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run"); //定义hklm指向注册表的LocalMachine,对注册表的结构,可以在windows的运行里,输入regedit,运行后,可以看看里面的各 个子键,其中Software/Microsoft/Windows/CurrentVersion/Run就是关系到系统中随系统启动而启动的程序,通 称启动项
    try
    {
     run.SetValue("hello.exe",@"F:/c#/hello/bin/Debug/hello.exe"); //将我们的程序加进去,系统启动时,hello.exe就会随系统启动而启动了,后面F:/C#....就这个程序的位置,你可以将hello.exe 换成你自己的,比如:notepad.exe注意修改这个程序的位置。至于"@"这个符号加在"F:/C#/hello/"之前的作用,是为了保证. net编译器,不将/解释为转换符,如果这里不用@的话,那就应该写成"F://C#//hello//",一个/就要改为两个//。
     MessageBox.Show ("添加注册表启动项成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //弹出信息框,提示,已经成功添加了。要了解MessageBox.Show的各参数意义,可以将光标放到其里面,按F1,.net的IDE(集成开发 环境)会有详细的文档显示出来,告诉您最权威详尽的解释。
     hklm.Close();} //注意,一定要关闭,注册表应用。
    catch(Exception my) //这是捕获异常的
    {
     MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
   }
   private void button2_Click(object sender, System.EventArgs e) //button1是添加,这个button2是删除。后面的实现都差不多
   {
   
    RegistryKey hklm=Registry.LocalMachine;
    RegistryKey run=hklm.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run");
    try
    {
     run.DeleteValue("hello.exe"); //这儿是关键的区别,删除hello.exe这个启动项键值
                               
     MessageBox.Show("移除注册表启动项成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
     hklm.Close();
     }
    catch(Exception my)
    {
     MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
   }
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值