///
/// 显示进度
///
///
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("http://www.111com.net/ update.zip", @"F:update.zip", ProgressBar_Value, null);
}
|