Beispiel #1
0
        private async void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = true;
            _cts?.Dispose();
            _cts = new CancellationTokenSource();
            var progress = new Progress <ProgressReport>();

            progress.ProgressChanged += (s, args) =>
            {
                progressBar1.Style   = ProgressBarStyle.Blocks;
                progressBar1.Maximum = (int)args.Total;
                progressBar1.Value   = (int)args.Downloaded;
            };

            //var url = @"https://download.microsoft.com/download/E/A/E/EAE6F7FC-767A-4038-A954-49B8B05D04EB/Express%2032BIT/SQLEXPR_x86_ENU.exe";
            var url2     = @"https://download.mozilla.org/?product=firefox-stub&os=win&lang=ru";
            var filename = DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".exe";
            var path     = Path.Combine(@"D:\", filename);

            progressBar1.Visible = true;
            progressBar1.Style   = ProgressBarStyle.Marquee;
            try
            {
                await _webClient.DownloadFileAsync(url2, path, progress, _cts.Token);

                _lblFilename.Text = path;
            }
            catch (OperationCanceledException)
            {
                button2.Enabled      = true;
                progressBar1.Visible = false;
                return;
            }
            button2.Enabled = true;
        }