不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
WinForm下载文件并显示下载进度示例
编辑:dnawo 日期:2014-07-17
WinForm下载文件并显示下载进度示例
复制内容到剪贴板 程序代码
/// <summary>
/// 显示进度
/// </summary>
/// <param name="val"></param>
private void ProgressBar_Value(int val)
{
progressBar1.Value = val;
label1.Text = val.ToString() + "%";
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="savefile"></param>
/// <param name="downloadProgressChanged"></param>
/// <param name="downloadFileCompleted"></param>
private void DownloadFile(string url, string savefile, Action<int> 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
/// <summary>
/// 点击下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DownloadFile("http://www.mzwu.com/update.zip", @"F:\update.zip", ProgressBar_Value, null);
}
/// 显示进度
/// </summary>
/// <param name="val"></param>
private void ProgressBar_Value(int val)
{
progressBar1.Value = val;
label1.Text = val.ToString() + "%";
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="savefile"></param>
/// <param name="downloadProgressChanged"></param>
/// <param name="downloadFileCompleted"></param>
private void DownloadFile(string url, string savefile, Action<int> 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
/// <summary>
/// 点击下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DownloadFile("http://www.mzwu.com/update.zip", @"F:\update.zip", ProgressBar_Value, null);
}
参考资料
[1].How to download a file in C#:http://www.fluxbytes.com/csharp/how-to-download-a-file-in-c-progressbar-and-download-speed/
[2].Download File Asynchronously With ProgressBar:http://www.dreamincode.net/forums/topic/115491-download-file-asynchronously-with-progressbar/
[3].Asynchronous File Download with Progress Bar:http://stackoverflow.com/questions/9459225/asynchronous-file-download-with-progress-bar
评论: 0 | 引用: 0 | 查看次数: 3196
发表评论
请登录后再发表评论!