代碼如下:
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>