代码如下:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Windows.Forms;
using System.Data;
namespace CYSoft.CommonLib
{
public partial class WebDownloadFile : Component
{
#region 变量属性
/// <summary>
/// 下载过程进度事件
/// </summary>
/// <param name="e"></param>
public delegate void ProgressChangedEventHandler(DownLoadFileInfo fileInfo, int percentage);
public event ProgressChangedEventHandler ProgressChangedEvent;
/// <summary>
/// 下载完成
/// </summary>
/// <param name="fileInfo"></param>
/// <param name="e"></param>
public delegate void DownloadFileCompletedEventHandler();
public event DownloadFileCompletedEventHandler DownloadFileCompletedEvent;
/// <summary>
/// 一个文件下载完成
/// </summary>
/// <param name="fileInfo"></param>
/// <param name="e"></param>
public delegate void DownloadOneFileCompletedEventHandler(DownLoadFileInfo fileInfo);
public event DownloadOneFileCompletedEventHandler DownloadOneFileCompletedEvent;
/// <summary>
/// 需要下载的文件列表()
/// </summary>
private List<DownLoadFileInfo> m_DownLoadFiles = null;
public List<DownLoadFileInfo> DownLoadFiles
{
set { m_DownLoadFiles = value; }
get { return m_DownLoadFiles; }
}
private WebClient m_web = null;
/// <summary>
/// 当前下载的文件是否完成
/// </summary>
private bool m_isFinished = false;
/// <summary>
/// 当前下载文件信息
/// </summary>
private DownLoadFileInfo m_fileInfo = null;
/// <summary>
/// 已经下载文件个数
/// </summary>
private int m_downFileCount = 0;
/// <summary>
/// 下载是否完成
/// </summary>
public bool DownLoadFinished
{
get
{
if (DownLoadFiles != null && DownLoadFiles.Count == m_downFileCount)
return true;
else
return false;
}
}
#endregion
#region 构造函数
public WebDownloadFile()
{
InitializeComponent();
}
public WebDownloadFile(IContainer container)
{
container.Add(this);
InitializeComponent();
}
#endregion
#region 函数方法
/// <summary>
/// 开始下载
/// </summary>
public void StartDownload()
{
try
{
foreach (DownLoadFileInfo info in DownLoadFiles)
{
m_isFinished = false;
m_fileInfo = info;
m_web = new WebClient();
m_web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
m_web.DownloadFileCompleted += new AsyncCompletedEventHandler(web_DownloadFileCompleted);
//判断没有路径则创建一个
if (!Directory.Exists(info.fileSavePath))
Directory.CreateDirectory(info.fileSavePath);
string url = info.fileWebUrl;
string fileFullName = info.fileSavePath + "//" + info.fileName;
//判断文件已经存在则删除原有文件
if (File.Exists(fileFullName))
File.Delete(fileFullName);
m_web.DownloadFileAsync(new Uri(url), fileFullName);
while (!m_isFinished)
{
Application.DoEvents();
Thread.Sleep(1);
}
}
if (DownLoadFinished)
if (DownloadFileCompletedEvent != null)
DownloadFileCompletedEvent();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 下载进程事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
if (ProgressChangedEvent != null)
ProgressChangedEvent(m_fileInfo, e.ProgressPercentage);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 下载完成
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void web_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
if (e.Cancelled)
return;
if (e.Error == null)
m_downFileCount += 1;
if (DownloadOneFileCompletedEvent != null)
DownloadOneFileCompletedEvent(m_fileInfo);
}
//catch (WebException exp)
//{
// MessageBox.Show(String.Format("无法找到指定资源/n/n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
//catch (XmlException exp)
//{
// MessageBox.Show(String.Format("下载的升级文件有错误/n/n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
//catch (NotSupportedException exp)
//{
// MessageBox.Show(String.Format("升级地址配置错误/n/n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
//catch (ArgumentException exp)
//{
// MessageBox.Show(String.Format("下载的升级文件有错误/n/n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
//catch (Exception exp)
//{
// MessageBox.Show(String.Format("升级过程中发生错误/n/n{0}", exp.Message), "自动升级", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
catch (Exception ex)
{ throw ex; }
finally { m_isFinished = true; }
}
#endregion
#region DownLoadFileInfo
public class DownLoadFileInfo
{
/// <summary>
/// 文件标识
/// </summary>
public string fileID = null;
/// <summary>
/// 文件名(不包括路径)
/// </summary>
public string fileName = null;
/// <summary>
/// 文件WEB地址
/// </summary>
public string fileWebUrl = null;
/// <summary>
/// 文件保存到本地的路径
/// </summary>
public string fileSavePath = null;
}
#endregion
}
}
WebService提供方法
[WebMethod]
public string GetUpdataConfig()
{
string file = Server.MapPath("//UpdateFiles//UpdateConfig.xml");
XmlDocument xd = new XmlDocument();
xd.Load(file);
return xd.InnerXml;
}
UpdateConfig.xml内容:
<?xml version="1.0" encoding="utf-8"?>
<updateFiles>
<file FileID="CA80EA43-391F-434E-A7F7-4FC96FA50163" FileInfo="UI-1.dll" FileName="UI-0901091.dll" Url="http://127.0.0.1/WebSrvTest/UpdateFiles/UI-0901091.rar" SavePath="test/tmp/" Version="1.0.0.1" Size="26009" Restart="true" />
<file FileID="EB544DEC-2A1D-4413-B7EB-7E306EF3A20E" FileInfo="UI-2.dll" FileName="UI-0901092.dll" Url="http://127.0.0.0/WebSrvTest/UpdateFiles/UI-0901092.rar" SavePath="test/tmp/" Version="1.0.0.2" Size="26009" Restart="true" />
</updateFiles>