C# WinForm 开机自动运行
第一种方法:
代码
using Microsoft.Win32;
private void Form1_Load(object sender, EventArgs e)
{
//获取程序执行路径..
string starupPath = Application.ExecutablePath;
//class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
try
{
//SetValue:存储值的名称
run.SetValue("WinForm",starupPath);
MessageBox.Show(" 注册表添加成功!!",""提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
loca.Close();
}
catch(Exception ee)
{
MessageBox.Show(ee.Message.ToString(),"提 示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//获取程序执行路径..
string starupPath = Application.ExecutablePath;
//class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
try
{
//SetValue:存储值的名称
run.SetValue("WinForm",starupPath);
MessageBox.Show(" 注册表添加成功!!",""提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
loca.Close();
}
catch(Exception ee)
{
MessageBox.Show(ee.Message.ToString(),"提 示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
第二种方法:
添加到注册表里直接用代码写到注册表里,也可以手动添加.
E:D:\\tractor.exe//可以是你的程序名和 完整路径就OK了.
也可以手动拖到启动里面....
代码
RegistryKey hklm = Application.LocalMachine;
RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Current\Version\Run");
try
{
run.SetValue("tractor.exe","D:\\tractor.exe");
MessageBox.Show("注册表添加成功!!","提示",MessageBoxButton.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch(Exception ee)
{
MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButton.OK, MessageBoxIcon.Error);
}
RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\Current\Version\Run");
try
{
run.SetValue("tractor.exe","D:\\tractor.exe");
MessageBox.Show("注册表添加成功!!","提示",MessageBoxButton.OK, MessageBoxIcon.Information);
hklm.Close();
}
catch(Exception ee)
{
MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButton.OK, MessageBoxIcon.Error);
}