コード例 #1
0
        private bool SetWindowsUpdate(bool enable)
        {
            if (OSVersion.FriendlyName().Contains("10"))
            {
                //Stop per Windows Service on windows 10
                ServiceController service = new ServiceController("wuauserv");
                try
                {
                    TimeSpan timeout = TimeSpan.FromMilliseconds(500);
                    if (enable)
                    {
                        service.Start();
                        //service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    }
                    else
                    {
                        service.Stop();
                        //service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                    }

                    Logger.Log(String.Format("Set windows update to {0}", enable));

                    return(true);
                }
                catch
                {
                    return(false);
                }
            }

            //Use Automatic Update Class
            try
            {
                var auc = new AutomaticUpdatesClass();

                if (enable)
                {
                    auc.Resume();
                }
                else
                {
                    auc.Pause();
                }

                Logger.Log(String.Format("Set windows update to {0}", enable));

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #2
0
        private bool SetWindowsUpdate(bool enable)
        {
            if (OSVersion.FriendlyName().Contains("10"))
            {
                //Stop per Windows Service on windows 10
                ServiceController service = new ServiceController("wuauserv");
                try
                {
                    TimeSpan timeout = TimeSpan.FromMilliseconds(500);
                    if (enable)
                    {
                        if (service.Status != ServiceControllerStatus.StartPending && service.Status != ServiceControllerStatus.Running)
                        {
                            service.Start();
                            Logger.Log($"Started service '{service.DisplayName}'.");
                            //service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                        }
                        else
                        {
                            Logger.Log($"Service '{service.DisplayName}' is already starting / running...");
                        }
                    }
                    else
                    {
                        if (service.Status != ServiceControllerStatus.StopPending && service.Status != ServiceControllerStatus.Stopped)
                        {
                            service.Stop();
                            Logger.Log($"Stopped service '{service.DisplayName}'.");
                            //service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                        }
                        else
                        {
                            Logger.Log($"Service '{service.DisplayName}' is already stopping / stopped...");
                        }
                    }

                    Logger.Log(String.Format("Set windows update to {0}", enable));

                    return(true);
                }
                catch (Exception ex)
                {
                    Logger.Log(string.Format("Unable to disable Windows Update: {0} : ", ex.Message, ex.StackTrace));
                    return(false);
                }
            }

            //Use Automatic Update Class
            try
            {
                var auc = new AutomaticUpdates();

                if (enable)
                {
                    auc.Resume();
                }
                else
                {
                    auc.Pause();
                }

                Logger.Log(String.Format("Set windows update to {0}", enable));

                return(true);
            }
            catch (Exception ex)
            {
                Logger.Log(string.Format("Unable to disable Windows Update: {0} : ", ex.Message, ex.StackTrace));
                return(false);
            }
        }