如何开机时自动运行程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
......
....
....
private void button1_Click(object sender, System.EventArgs e)
{//浏览执行文件
this.openFileDialog1.ShowDialog();
if(this.openFileDialog1.FileName.Length>0)
{
this.textBox1.Text=this.openFileDialog1.FileName;
}
}
private void button2_Click(object sender, System.EventArgs e)
{//设置自启动程序
if(this.textBox1.Text.Length<1)
{
MessageBox.Show("请首先浏览程序文件选择一个执行程序!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
try
{
string FileName=this.textBox1.Text;
string ShortFileName=FileName.Substring(FileName.LastIndexOf("\\")+1);
//打开子键节点
RegistryKey MyReg=Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);
if(MyReg==null)
{//如果子键节点不存在,则创建之
MyReg=Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
//在注册表中设置自启动程序
MyReg.SetValue(ShortFileName,FileName);
MessageBox.Show("设置自启动程序操作成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(Exception Err)
{
MessageBox.Show("写注册表操作发生错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
OpenFileDialog 的一些属性的设置
this.openFileDialog1.Filter = "所有执行程序(*.exe)|*.exe";
this.openFileDialog1.InitialDirectory = "C:";
this.openFileDialog1.RestoreDirectory = true;
this.openFileDialog1.Title = "选择执行程序";