如何实现上位机程序开机自动启动呢?
实现很简单,通过C#操作注册表来实现。
代码已经写好,直接复制过去,在主程序里调用一下即可。
/// <summary>
/// 修改程序在注册表中的键值
/// </summary>
/// <param name="isAuto">true:开机启动,false:不开机自启</param>
private void AutoStart(bool isAuto = true)
{
if (isAuto == true)
{
RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); R_run.SetValue("ProjectName", System.Windows.Forms.Application.ExecutablePath);
R_run.Close();
R_local.Close();
}
else {
RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.DeleteValue("ProjectName", false);
R_run.Close();
R_local.Close();
}
}