Exemplo n.º 1
0
        private void SetStatusAndSummary()
        {
            this.autoDownloadStatus = this.WindowsStoreAutoDownloadConfigurator.Get();

            switch (this.AutoDownloadStatus)
            {
            case WindowsStoreAutoDownload.Default:
                this.Status  = RecommendationStatus.Success;
                this.Summary = "Default policy applies.";
                break;

            case WindowsStoreAutoDownload.Always:
                this.Status  = RecommendationStatus.Success;
                this.Summary = "Automatic download is enabled.";
                break;

            case WindowsStoreAutoDownload.Never:
                this.Status  = RecommendationStatus.Success;
                this.Summary = "Automatic download is disabled.";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
    public bool Set(WindowsStoreAutoDownload status)
    {
        var    newValue = (int)status;
        string cmd;

        switch (newValue)
        {
        case 4:
        case 2:
            if ((int)this.Get() == newValue)
            {
                return(true);
            }

            cmd = @"ADD HKLM\SOFTWARE\Policies\Microsoft\WindowsStore /v AutoDownload /t REG_DWORD /f /d " + newValue;
            break;

        default:
            if (this.Get() == WindowsStoreAutoDownload.Default)
            {
                return(true);
            }

            cmd = @"DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\WindowsStore /f /v AutoDownload";
            break;
        }

        var psi = new ProcessStartInfo("reg.exe")
        {
            Arguments        = cmd,
            UseShellExecute  = true,
            WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System),
            Verb             = "runas",
#if !DEBUG
            WindowStyle    = ProcessWindowStyle.Hidden,
            CreateNoWindow = true
#endif
        };

        try
        {
            var p = Process.Start(psi);
            if (p == null)
            {
                return(false);
            }

            if (!p.WaitForExit(2000))
            {
                return(false);
            }

            if (p.ExitCode != 0)
            {
                return(false);
            }
        }
        catch (Exception)
        {
            return(false);
        }

        return(this.Get() == status);
    }