asp.net WinForm下载文件并显示下载进度条教程

作者:袖梨 2022-06-25

asp.net WinForm下载文件并显示下载进度条教程

WinForm下载文件并显示下载进度示例

代码如下 复制代码

///


/// 显示进度
///

///
private void ProgressBar_Value(int val)
{
progressBar1.Value = val;
label1.Text = val.ToString() + "%";
}

///


/// 下载文件
///

///
///
///
///
private void DownloadFile(string url, string savefile, Action downloadProgressChanged, Action downloadFileCompleted)
{
WebClient client = new WebClient();
if (downloadProgressChanged != null)
{
client.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
{
this.Invoke(downloadProgressChanged, e.ProgressPercentage);
};
}
if (downloadFileCompleted != null)
{
client.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e)
{
this.Invoke(downloadFileCompleted);
};
}
client.DownloadFileAsync(new Uri(url), savefile);
}
delegate void Action(); //.NET Framework 2.0得自定义委托Action

///


/// 点击下载
///

///
///
private void button1_Click(object sender, EventArgs e)
{
DownloadFile("https://www.111cn.net/ update.zip", @"F:update.zip", ProgressBar_Value, null);
}

相关文章

精彩推荐