zoukankan      html  css  js  c++  java
  • 彻底关闭Win10自动更新的代码

    Windows 10 总是反反复复无休止的更新,然后自动(闲时)重启或者提示重启, 简直烦不胜烦...

    所以直接写了这个小程序,关闭所有自动更新项,一劳永逸.

           static void Main(string[] args)
            {
                Console.Title = "Disable Windows AutoUpdate";
                Console.WriteLine("Disable Windows 10 AutoUpdate");
                Console.WriteLine("Version:1.0");
                Console.WriteLine("Release:2021-01-21");
    
                DisableSvc();
    
                DisableReg();
                // 注册表修改或者手动处理:
                // 组策略:gpedit.msc -> 计算机配置 -> 管理模板 -> Windows组件 -> Windows更新:
                //    配置自动更新 -> 已禁用
                //    删除使用所有Windows更新功能的访问权限 -> 已启用
    
                DisableTaskScheduler();
    
                Console.WriteLine("All Windows Auto Update configuration was disabled.");
                Console.WriteLine("Please restart the system manually for the configuration to take effect.");
                Console.WriteLine("Press any key to exit...");
    
                Console.Read();
    
            }
    
    
            private static void DisableTaskScheduler()
            {
                try
                {
                    TaskSchedulerClass scheduler = new TaskSchedulerClass();
                    scheduler.Connect();
                    ITaskFolder folder = scheduler.GetFolder(@"MicrosoftWindowsWindowsUpdate");
                    foreach (IRegisteredTask task in folder.GetTasks(0))
                    {
                        task.Stop(0);
                        task.Enabled = false;
                        Console.WriteLine($"TaskSchedule [{task.Name}] was disabled.");
                    }
                    // eg. Scheduled Start
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    
            private static void DisableReg()
            {
                // 注册还有个位置,但是不好判断,暂时不处理
                SetRegVal(
                    Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,
                        Microsoft.Win32.RegistryView.Default), @"SOFTWAREPoliciesMicrosoftWindowsWindowsUpdateAU", "NoAutoUpdate", 1);
                // HKEY_USERSS-1-5-21-897350936-3504488752-3495779238-500SOFTWAREMicrosoftWindowsCurrentVersionGroup Policy Objects{C3BF0CDD-7FAF-4D58-BCA4-0A011084C32E}MachineSoftwarePoliciesMicrosoftWindowsWindowsUpdateAU
                Console.WriteLine($"Group Policy [WindowsUpdate\NoAutoUpdate] was disabled.");
                SetRegVal(
                    Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,
                        Microsoft.Win32.RegistryView.Default), @"SOFTWAREPoliciesMicrosoftWindowsWindowsUpdate", "SetDisableUXWUAccess", 1);
                // HKEY_USERSS-1-5-21-897350936-3504488752-3495779238-500SOFTWAREMicrosoftWindowsCurrentVersionGroup Policy Objects{C3BF0CDD-7FAF-4D58-BCA4-0A011084C32E}MachineSoftwarePoliciesMicrosoftWindowsWindowsUpdate
                Console.WriteLine($"Group Policy [WindowsUpdate\SetDisableUXWUAccess] was enabled.");
            }
    
            private static void DisableSvc()
            {
                string[] svrs = new[] { @"wuauserv", @"UsoSvc", @"WaaSMedicSvc" };
                foreach (var svr in System.ServiceProcess.ServiceController.GetServices().Where(q => svrs.Contains(q.ServiceName)))
                {
                    try
                    {
                        SetSvcVal(svr.ServiceName, "Start", 4);
                        if (svr.Status==ServiceControllerStatus.Running)
                        {
                            svr.Stop();
                        }
                        Console.WriteLine($"Windows Service [{svr.DisplayName}] was disabled.");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }

    后续可以通过命令,查看系统是否还会经常自动重启?

    systeminfo | find "系统启动时间"

  • 相关阅读:
    C#检查数组是否重复——HashSet
    C#动态生成控件,并添加事件处理,获取控件值并保存
    .net接口交互
    SQL Server 表建Trigger
    SQL Server 表建Trigger
    SQL语句修改not in 变为not exists
    奋战杭电ACM(DAY11)1017
    奋战杭电ACM(DAY11)1016
    奋战杭电ACM(DAY10)1015
    奋战杭电ACM(DAY9)1014
  • 原文地址:https://www.cnblogs.com/honk/p/14308907.html
Copyright © 2011-2022 走看看