This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/UpTool2/Tool/DownloadDialog.cs

43 lines
1.2 KiB
C#

using System;
using System.Net;
using System.Windows.Forms;
namespace UpTool2.Tool
{
public partial class DownloadDialog : Form
{
private readonly WebClient _client;
private bool _close;
public byte[] Result;
public DownloadDialog(string uri)
{
InitializeComponent();
try
{
_client = new WebClient();
_client.DownloadProgressChanged += ProgressChanged;
_client.DownloadDataCompleted += Done;
_client.DownloadDataAsync(new Uri(uri), _client);
}
catch
{
DialogResult = DialogResult.Abort;
Close();
}
}
private void Done(object sender, DownloadDataCompletedEventArgs e)
{
DialogResult = DialogResult.OK;
_close = true;
Result = e.Result;
Close();
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) =>
progressBar.Value = e.ProgressPercentage;
private void DownloadDialog_FormClosing(object sender, FormClosingEventArgs e) => e.Cancel = !_close;
}
}