如何用C#语言通过修改注册表把一个程序添加到开机启动项 如何用C#语言通过修改注册表把一个程序添加到开机启动项 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加上 using Microsoft.Win32;public static bool SetAutoRun(string keyName,string filePath) { try { RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true); runKey.SetValue(keyName,filePath); runKey.Close(); } catch { return false; } return true; }test: SetAutoRun("myexe","c:\\text.exe"); 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); } }}} Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);[为什么,我返回的都是 null 非常感谢你提供的源码我成功做出了一个添加和删除启动项的WinForm小程序非常之感谢 C#如何激活任务栏窗口? 文件夹存贮文件的极限数以及检索时间问题 关于XML文件的删除节点 C/S ,客户端直接连接远程数据库 ,都有什么缺点。 如何向数组添加元素 jmail 水晶报表的问题 问个数学问题,斑竹请手下留情,不要扇偶! 如何获取一个Excel的Sheet的名称 请帮忙 行 66: if((float)myValues[3]>max)max=(float)myValues[3]; 出错 ASP.NET获取GridView中选中行的值 用C#/VS2003开发的一个组件,在C#/VS2005、C#/VS2008下不能使用中文输入法,该如何解决???
{
try
{
RegistryKey runKey=Registry.LocalMachine.OpenSubKey(@"\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
runKey.SetValue(keyName,filePath);
runKey.Close();
}
catch
{
return false;
}
return true;
}
test: SetAutoRun("myexe","c:\\text.exe");
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);
}
}
}
}
为什么,我返回的都是 null
我成功做出了一个添加和删除启动项的WinForm小程序
非常之感谢