写“连载”第二天,C#对注册表的启动项操作。

2005年11月1日  星期二  20:38:17
神舟天运M363S这款本本,还不错,4999的价格,1.4G的CPU,512内存。40GB硬盘,14.1LCD。还带COMBO光驱。
今天股指下探,尾市回升,但还是以小阴收盘。
宝钢权证,承接昨天的涨势,不过,在最后半小时,上演了大跳水,最后跌到1元以下了。盘中也是呈振荡出货的走势。
将文章发到了blog.csdn.net/metababy和网易日记上了。MSN的那个本来不错的,可是其帮助文档上写要删除一些旧文档。
在csdn.net上无意间看到了邹建的新书SQL实例之类的。才知道,以前成都MVPQQ群里所说的邹建原来是这么有能力的人。他在成都工作,在csdn.net上文章很多的。主要是SQL类的,从2003.11一直连任MVP。是SQL的大版主。
看来我得多多学习。就按以前的规距,不图名利,以潜心研究的心态对待编程,从艺术的角度欣赏自己的代码。
是我的工作是编程,多好啊,那进步就快多了。我现在作记件的编目工作,一本书一本书的编,一天到晚,属于自己的时间很少。只有挤上再挤了。

今天的就先写一个简单的注册表启动项的程序吧。
以下是代码和注释,程度我已经调试通过了的,请放心使用。

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);
   }
  }
 }
}
//源代码,结束。
有用词不当,和解释得有错的地方,欢迎指正,共同学习探讨。
也欢迎光临我的淘宝小店http://shop1471977.taobao.com

对于程序,在哪儿不确定的,可以用到这个
Application.ExecutablePath 获得路径和文件名
其在msdn上的说明:
Application.ExecutablePath 属性  [C#]

获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。


[C#]
public static string ExecutablePath {get;}

属性值
启动了应用程序的可执行文件的路径和可执行文件的名称。

http://riji.163.com/weblog/page/metababy
http://blog.csdn.net/metababy
http://spaces.msn.com/members/metababy
http://shop1471977.taobao.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值