如何用C#语句把checkbox添加到开机启动项里,谢谢
解决方案 »
- 某个windows服务的控制权限能否指定到某用户?
- [难度][高分] 请教如何获取拉姆达表达式的值
- 正则表达式 ?:
- 如何解决“值不能未空,参数名:datdSet()“这个异常???
- c# 中怎么使combobox里面输入为大写
- 使用access数据库时遇到的一个弱智问题!请帮我看看!先谢了~~~~~~~~~~~~~~~~~~~~~~~~~~
- 求backgroudworker控件的用法
- 求代码~~~~~~~~~有关IE快捷键的!!!,分不够可以加
- ASP.NET WEB开发怎么从网页打开另一个网页并获取数据呢?
- WinXP中VSNET调试出现的“不能显示窗口”的问题
- StreamWriter不能写入问题!!急!!
- 如何通过C#.NET中的反射机制来创建C#.NET泛型类的实例,并调用其方法??
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);
}
}
}
}