如何用C#语句把checkbox添加到开机启动项里,谢谢

解决方案 »

  1.   

    把checkbox添加到开机启动项里--是什么意思?
      

  2.   

    我是意思是把checkbox的值等于true时,我下在开机的时候,自动动行程序,我是一个新手,不知道问题怎么说你们才能明白,多多包涵
      

  3.   

    我是意思是当checkbox的值等于true时,我下在开机的时候,自动运行程序,我是一个新手,不知道问题怎么说你们才能明白,多多包涵
      

  4.   

    我意思是当checkbox值等于true时,添加到开机启动项里,下次开机的时候就不用手动去运行了,而是让程序开机后一直运行本程序.
      

  5.   


    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);
        }
       }
    }
    }