命名空间:System.Net
程序集: System(在 System.dll 中)
语法:public event DownloadProgressChangedEventHandler DownloadProgressChanged
其中,WebClient提供很多触发事件,这里着重说明两个事件,也是最常用到的两个事件,即DownloadFileCompleted、DownloadProgressChanged 。
下面的代码示例演示如何为此事件设置事件处理程序:
代码
// Sample call : DownLoadFileInSoftware (http://www.cnblogs.com/xvqm00);
public static void DownLoadFileInSoftware ()
{
///<下载新版本安装包
Uri uri = new Uri(this.SourcePath, UriKind.Absolute);
WebClient client = new WebClient();
//当文件下载完成后触发
client.DownloadFileCompleted+=new AsyncCompletedEventHandler(client_DownloadFileCompleted);
//进度条
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
client.DownloadFileAsync(uri, this.SavePath);
}
public static void DownLoadFileInSoftware ()
{
///<下载新版本安装包
Uri uri = new Uri(this.SourcePath, UriKind.Absolute);
WebClient client = new WebClient();
//当文件下载完成后触发
client.DownloadFileCompleted+=new AsyncCompletedEventHandler(client_DownloadFileCompleted);
//进度条
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
client.DownloadFileAsync(uri, this.SavePath);
}
以上代码中有两个参数是需要注意的
1,this.SourcePath指的是“下载资料文件地址”
2,this.SavePath则指的是“下载文件本地存放路径”
另外,UriKind.Absolute指的是此uri是绝对路径。(通常采用相对路径较好!)
因而,下载期间,你要处理的进度及下载完成后是否给预提醒,均要靠DownloadFileCompleted、DownloadProgressChanged 这两个事件了。
下载完成后触发
/// <summary>
/// 下载完成后触发
/// </summary>
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
///<运行安装并关闭本程序
bool bKill = true;
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.ProcessName == sProcessName)
{
bKill = false;
if (MessageBox.Show("程序正在运行,安装前请关闭程序,是否立即关闭?", "在线升级", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
== DialogResult.OK)
{
p.Kill();
bKill = true;
break;
}
}
}
if (bKill)
{
Process p = new Process();
p.StartInfo.FileName = this.SavePath;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.StartInfo.WorkingDirectory = Application.StartupPath;
p.Start();
}
else
{
lblMsgInfo.Text = "更新未完成!";
}
}
catch { throw; }
}
/// 下载完成后触发
/// </summary>
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
///<运行安装并关闭本程序
bool bKill = true;
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.ProcessName == sProcessName)
{
bKill = false;
if (MessageBox.Show("程序正在运行,安装前请关闭程序,是否立即关闭?", "在线升级", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
== DialogResult.OK)
{
p.Kill();
bKill = true;
break;
}
}
}
if (bKill)
{
Process p = new Process();
p.StartInfo.FileName = this.SavePath;
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.StartInfo.WorkingDirectory = Application.StartupPath;
p.Start();
}
else
{
lblMsgInfo.Text = "更新未完成!";
}
}
catch { throw; }
}
进度条
/// <summary>
/// 进度条
/// </summary>
private void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
this.progressBar1.Maximum = (int)e.TotalBytesToReceive;
this.progressBar1.Value = (int)e.BytesReceived;
//percentage = Math.Ceiling(((decimal)e.BytesReceived / (decimal)e.TotalBytesToReceive) * 100);
this.lblPercent.Text = e.ProgressPercentage + "%";
}
catch
{
throw;
}
//if (e.BytesReceived == e.TotalBytesToReceive)
//{
// ////label2.Text = "总字节数: " + e.TotalBytesToReceive + " 进度的百分比: " + e.ProgressPercentage + "获取字节数: " + e.BytesReceived;
//}
}
/// 进度条
/// </summary>
private void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
this.progressBar1.Maximum = (int)e.TotalBytesToReceive;
this.progressBar1.Value = (int)e.BytesReceived;
//percentage = Math.Ceiling(((decimal)e.BytesReceived / (decimal)e.TotalBytesToReceive) * 100);
this.lblPercent.Text = e.ProgressPercentage + "%";
}
catch
{
throw;
}
//if (e.BytesReceived == e.TotalBytesToReceive)
//{
// ////label2.Text = "总字节数: " + e.TotalBytesToReceive + " 进度的百分比: " + e.ProgressPercentage + "获取字节数: " + e.BytesReceived;
//}
}
这是我做的一个示例,没什么特别的东西,不作多余解释。