开机自启动的方法,若返回失败,请尝试使用管理员权限执行!
/// <summary> /// 程序自启动设置 /// </summary> /// <param name="exeName">程序名称</param> /// <param name="path">程序路径</param> /// <param name="isAuto">是否自启动</param> /// <returns></returns> public static bool AutoStart(string exeName,string path, bool isAuto = true) { try { if (isAuto == true) { RegistryKey R_local = Registry.LocalMachine; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); R_run.SetValue(exeName, path); R_run.Close(); R_local.Close(); } else { RegistryKey R_local = Registry.LocalMachine; RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); R_run.DeleteValue(exeName, false); R_run.Close(); R_local.Close(); } return true; } catch (Exception) { return false; } }
评论前必须登录!
注册