彻底关闭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(@"\Microsoft\Windows\WindowsUpdate");
                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), @"SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU", "NoAutoUpdate", 1);
            // HKEY_USERS\S-1-5-21-897350936-3504488752-3495779238-500\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy Objects\{C3BF0CDD-7FAF-4D58-BCA4-0A011084C32E}Machine\Software\Policies\Microsoft\Windows\WindowsUpdate\AU
            Console.WriteLine($"Group Policy [WindowsUpdate\\NoAutoUpdate] was disabled.");
            SetRegVal(
                Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,
                    Microsoft.Win32.RegistryView.Default), @"SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate", "SetDisableUXWUAccess", 1);
            // HKEY_USERS\S-1-5-21-897350936-3504488752-3495779238-500\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy Objects\{C3BF0CDD-7FAF-4D58-BCA4-0A011084C32E}Machine\Software\Policies\Microsoft\Windows\WindowsUpdate
            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 "系统启动时间"

 

posted @ 2021-01-21 16:46  devs  阅读(1231)  评论(2编辑  收藏  举报