/// <summary> /// 添加到开机自动运行 /// </summary> public static void BootToRunAuto() { //获取可执行文件的全部路径 string exeDir = System.Windows.Forms.Application.ExecutablePath; //获取Run键 RegistryKey key1 = Registry.CurrentUser; RegistryKey key2 = key1.CreateSubKey("SOFTWARE"); RegistryKey key3 = key2.CreateSubKey("Microsoft"); RegistryKey key4 = key3.CreateSubKey("Windows"); RegistryKey key5 = key4.CreateSubKey("CurrentVersion"); RegistryKey key6 = key5.CreateSubKey("Run"); //在Run键中写入一个新的键值 if (key6.GetValue("PrintERP") == null || key6.GetValue("PrintERP").ToString()!="false") key6.SetValue("PrintERP", exeDir); } /// <summary> /// 取消添加开机自动运行 /// </summary> public static void CancleBootRunAuto() { //获取可执行文件的全部路径 string exeDir = System.Windows.Forms.Application.ExecutablePath; //获取Run键 RegistryKey key1 = Registry.CurrentUser; RegistryKey key2 = key1.CreateSubKey("SOFTWARE"); RegistryKey key3 = key2.CreateSubKey("Microsoft"); RegistryKey key4 = key3.CreateSubKey("Windows"); RegistryKey key5 = key4.CreateSubKey("CurrentVersion"); RegistryKey key6 = key5.CreateSubKey("Run"); if (CheckKeyExists(key6, "PrintERP")) { //取消自启动 key6.SetValue("PrintERP", false); } } /// <summary> /// 检查注册表项是否存在 /// </summary> public static bool CheckKeyExists(RegistryKey key, string keyName) { bool isexists = false; foreach (string keyname in key.GetValueNames()) { if (keyname.Equals(keyName)) { isexists = true; break; } } return isexists; }
相关文章: