我们在做上位机的时候会用到我们的界面需要开机就启动。经过本人网络搜集".net工控上位机"公众号发表的这代码好用。只需要修改isAuto设定为true:开机启动,设定为false:不开机自启
代码如下:
///
/// 修改程序在注册表中的键值
///
/// true:开机启动,false:不开机自启
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();
}
}
需要的小伙伴快试试把,好用记得来留个言。