本文转载连接: https://www.cnblogs.com/wuqiuxue/p/7680430.html
将exe应用程序设置为开机启动,有多种方法,我们主要通过注册表设置开机启动选项。
using Microsoft.Win32; using System.Windows.Forms;
static void Main(string[] args)
{
string exepath = Application.ExecutablePath;
Console.WriteLine(exepath);
RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");//添加到所有用户的注册表启动项
RKey.SetValue("Myself_exe", exepath);
RKey.Close();
Console.ReadKey();
}
static void Main(string[] args)
{
int sleepsec = args.Length > 0 ? Convert.ToInt32(args[0]) : 0;
Console.WriteLine(sleepsec);
string exepath = Application.ExecutablePath;
Console.WriteLine(exepath);
RegistryKey RKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);//添加当前登录用户的注册表启动项
RKey.SetValue("self_exe", string.Format("{0} 30", exepath), RegistryValueKind.String);
RKey.Close();
Console.ReadKey();
}
添加所有用户注册的地址:
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run
添加当前用户注册的地址:
计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
RKey.DeleteValue(exepath);//删除注册表 RKey.Close();
注释:通过添加所有用户的注册表启动项(LocalMachine)会有权限问题,下次开机的时候会报异常,解决办法是将exe可执行文件更改为以管理员模式启动此程序。
优点:通过注册表传参的方式,在开机启动的时候会带入参数的数值,但是只要开过机,程序的二次启动就不会带入参数。所以可以利用这个特性,我们可以让程序做个休眠,因为开机启动的程序过多,会对电脑造成过大负荷。
1975

被折叠的 条评论
为什么被折叠?



