最近单位开发一个项目,其中需要用到自动升级功能。于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件,并添加少量代码,即可实现自动升级功能。因为我们的程序中可能包含多个exe或者dll文件,所以要支持多文件的更新。
这是本人第一次写比较复杂的文章,表达不清之处,请各位见谅。好,闲话少说,入正题。
最近单位开发一个项目,其中需要用到自动升级功能。因为自动升级是一个比较常用的功能,可能会在很多程序中用到,于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件,并添加少量代码,即可实现自动升级功能。因为我们的程序中可能包含多个exe或者dll文件,所以要支持多文件的更新。
首先,要确定程序应该去哪里下载需要升级的文件。我选择了到指定的网站上去下载,这样比较简单,也通用一些。在这个网站上,需要放置一个当前描述最新文件列表的文件,我们估且叫它服务器配置文件。这个文件保存了当前最新文件的版本号(lastver),大小(size),下载地址(url),本地文件的保存路径(path),还有当更新了这个文件后,程序是否需要重新启动(needRestart)。这个文件大致如下:
updateservice.xml
<?xml version="1.0" encoding="utf-8"?>
<updateFiles>
<file path="AutoUpdater.dll" url="http://update.iyond.com/CompanyClientApplication/AutoUpdater.zip" lastver="1.0.0.0" size="28672" needRestart="true" />
<file path="CompanyClient.exe" url="http://update.iyond.com/CompanyClientApplication/CompanyClient.zip" lastver="1.1.0.0" size="888832 " needRestart="true" />
<file path="HappyFenClient.dll" url="http://update.iyond.com/CompanyClientApplication/HappyFenClient.zip" lastver="1.0.0.0" size="24576" needRestart="true" />
<file path="NetworkProvider.dll" url="http://update.iyond.com/CompanyClientApplication/NetworkProvider.zip" lastver="1.0.0.0" size="32768" needRestart="true" />
<file path="Utility.dll" url="http://update.iyond.com/CompanyClientApplication/Utility.zip" lastver="1.0.0.0" size="20480" needRestart="true" />
<file path="Wizard.dll" url="http://update.iyond.com/CompanyClientApplication/Wizard.zip" lastver="1.0.0.0" size="24576" needRestart="true" />
</updateFiles>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
同时,客户端也保存了一个需要升级的本地文件的列表,形式和服务器配置文件差不多,我们叫它本地配置文件。其中,<Enable>节点表示是否启用自动升级功能,<ServerUrl>表示服务器配置文件的地址。
update.config
<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Enabled>true</Enabled>
<ServerUrl>http://update.iyond.com/updateservice.xml</ServerUrl>
<UpdateFileList>
<LocalFile path="AutoUpdater.dll" lastver="1.0.0.0" size="28672" />
<LocalFile path="CompanyClient.exe" lastver="1.1.0.0" size="888832 " />
<LocalFile path="HappyFenClient.dll" lastver="1.0.0.0" size="24576" />
<LocalFile path="NetworkProvider.dll" lastver="1.0.0.0" size="32768" />
<LocalFile path="Utility.dll" lastver="1.0.0.0" size="20480" />
<LocalFile path="Wizard.dll" lastver="1.0.0.0" size="24576" />
</UpdateFileList>
</Config>
使用自动各级组件的程序在启动时,会去检查这个配置文件。如果发现有配置文件中的文件版本和本地配置文件中描述的文件版本不一致,则提示用户下载。同时,如果本地配置文件中某些文件在服务器配置文件的文件列表中不存在,则说明这个文件已经不需要了,需要删除。最后,当升级完成后,会更新本地配置文件。
我们先来看一下如何使用这个组件。
在程序的Program.cs的Main函数中:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AutoUpdater au = new AutoUpdater();
try
{
au.Update();
}
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);
}
Application.Run(new MainUI());
}
如上所示,只需要简单的几行代码,就可以实现自动升级功能了。
软件运行截图:
![](https://www.cnblogs.com/images/cnblogs_com/iyond/20070614114109046.png)
![](https://www.cnblogs.com/images/cnblogs_com/iyond/2.PNG)
![](https://www.cnblogs.com/images/cnblogs_com/iyond/20070614114131546.png)
下面,我们来详细说一下这个自动升级组件的实现。
先看一下类图:
![](https://www.cnblogs.com/images/cnblogs_com/iyond/20070614105420562.png)
AutoUpdater:自动升级的管理类,负责整体的自动升级功能的实现。
Config:配置类,负责管理本地配置文件。
DownloadConfirm:一个对话框,向用户显示需要升级的文件的列表,并允许用户选择是否马上升级。
DownloadFileInfo:要下载的文件的信息
DownloadProgress:一个对话框,显示下载进度。
DownloadProgress.ExitCallBack,
DownloadProgress.SetProcessBarCallBack,
DownloadProgress.ShowCurrentDownloadFileNameCallBack:由于.NET2.0不允许在一个线程中访问另一个线程的对象,所以需要通过委托来实现。
LocalFile:表示本地配置文件中的一个文件
RemoteFile:表示服务器配置文件中的一个文件。
UpdateFileList:一个集合,从List<LocalFile>继承
我们先整体看一下AutoUpdater.cs:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
AutoUpdater.cs
public class AutoUpdater
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
const string FILENAME = "update.config";
private Config config = null;
private bool bNeedRestart = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public AutoUpdater()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
config = Config.LoadConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检查新版本
/// </summary>
/// <exception cref="System.Net.WebException">无法找到指定资源</exception>
/// <exception cref="System.NotSupportException">升级地址配置错误</exception>
/// <exception cref="System.Xml.XmlException">下载的升级文件有错误</exception>
/// <exception cref="System.ArgumentException">下载的升级文件有错误</exception>
/// <exception cref="System.Excpetion">未知错误</exception>
/// <returns></returns>
public void Update()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!config.Enabled)
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* 请求Web服务器,得到当前最新版本的文件列表,格式同本地的FileList.xml。
* 与本地的FileList.xml比较,找到不同版本的文件
* 生成一个更新文件列表,开始DownloadProgress
* <UpdateFile>
* <File path="" url="" lastver="" size=""></File>
* </UpdateFile>
* path为相对于应用程序根目录的相对目录位置,包括文件名
*/
WebClient client = new WebClient();
string strXml = client.DownloadString(config.ServerUrl);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dictionary<string, RemoteFile> listRemotFile = ParseRemoteXml(strXml);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
List<DownloadFileInfo> downloadList = new List<DownloadFileInfo>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//某些文件不再需要了,删除
List<LocalFile> preDeleteFile = new List<LocalFile>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (LocalFile file in config.UpdateFileList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (listRemotFile.ContainsKey(file.Path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RemoteFile rf = listRemotFile[file.Path];
if (rf.LastVer != file.LastVer)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size));
file.LastVer = rf.LastVer;
file.Size = rf.Size;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (rf.NeedRestart)
bNeedRestart = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
listRemotFile.Remove(file.Path);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
preDeleteFile.Add(file);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (RemoteFile file in listRemotFile.Values)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size));
config.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (file.NeedRestart)
bNeedRestart = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (downloadList.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DownloadConfirm dc = new DownloadConfirm(downloadList);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this.OnShow != null)
this.OnShow();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (DialogResult.OK == dc.ShowDialog())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (LocalFile file in preDeleteFile)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.Path);
if (File.Exists(filePath))
File.Delete(filePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
config.UpdateFileList.Remove(file);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
StartDownload(downloadList);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void StartDownload(List<DownloadFileInfo> downloadList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DownloadProgress dp = new DownloadProgress(downloadList);
if (dp.ShowDialog() == DialogResult.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//更新成功
config.SaveConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (bNeedRestart)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("程序需要重新启动才能应用更新,请点击确定重新启动程序。", "自动更新", MessageBoxButtons.OK, MessageBoxIcon.Information);
Process.Start(Application.ExecutablePath);
Environment.Exit(0);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Dictionary<string, RemoteFile> ParseRemoteXml(string xml)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Dictionary<string, RemoteFile> list = new Dictionary<string, RemoteFile>();
foreach (XmlNode node in document.DocumentElement.ChildNodes)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
list.Add(node.Attributes["path"].Value, new RemoteFile(node));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return list;
}
public event ShowHandler OnShow;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在构造函数中,我们先要加载配置文件:
public AutoUpdater()
{
config = Config.LoadConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME));
}
最主要的就是Update()这个函数了。当程序调用au.Update时,首先检查当前是否开户了自动更新:
if (!config.Enabled)
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果启用了自动更新,就需要去下载服务器配置文件了:
WebClient client = new WebClient();
string strXml = client.DownloadString(config.ServerUrl);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
然后,解析服务器配置文件到一个Dictionary中:
Dictionary<string, RemoteFile> listRemotFile = ParseRemoteXml(strXml);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
接下来比较服务器配置文件和本地配置文件,找出需要下载的文件和本地需要删除的文件:
List<DownloadFileInfo> downloadList = new List<DownloadFileInfo>();
//某些文件不再需要了,删除
List<LocalFile> preDeleteFile = new List<LocalFile>();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
foreach (LocalFile file in config.UpdateFileList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (listRemotFile.ContainsKey(file.Path))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RemoteFile rf = listRemotFile[file.Path];
if (rf.LastVer != file.LastVer)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size));
file.LastVer = rf.LastVer;
file.Size = rf.Size;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (rf.NeedRestart)
bNeedRestart = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
listRemotFile.Remove(file.Path);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
preDeleteFile.Add(file);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
foreach (RemoteFile file in listRemotFile.Values)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size));
config.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (file.NeedRestart)
bNeedRestart = true;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果发现有需要下载的文件,则向用户显示这些文件,并提示其是否马上更新。如果用户选择了马上更新,则先删除本地不再需要的文件,然后开始下载更新文件。
if (downloadList.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DownloadConfirm dc = new DownloadConfirm(downloadList);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this.OnShow != null)
this.OnShow();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (DialogResult.OK == dc.ShowDialog())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (LocalFile file in preDeleteFile)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.Path);
if (File.Exists(filePath))
File.Delete(filePath);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
config.UpdateFileList.Remove(file);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
StartDownload(downloadList);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们再来看一下StartDownload函数
private void StartDownload(List<DownloadFileInfo> downloadList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DownloadProgress dp = new DownloadProgress(downloadList);
if (dp.ShowDialog() == DialogResult.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//更新成功
config.SaveConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (bNeedRestart)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("程序需要重新启动才能应用更新,请点击确定重新启动程序。", "自动更新", MessageBoxButtons.OK, MessageBoxIcon.Information);
Process.Start(Application.ExecutablePath);
Environment.Exit(0);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在这个函数中,先调用DownloadProgress下载所有需要下载的文件,然后更新本地配置文件,最后,如果发现某些更新文件需要重新启动应用程序的话,会提示用户重新启动程序。
至此,AutoUpdater这个类的使命就完成了,其实,整个的升级过程也就完成了。(废话)。
最后,我们来看一下这个组件是如何下载更新文件的
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
DownloadProgress.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Diagnostics;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Iyond.Utility
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class DownloadProgress : Form
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private bool isFinished = false;
private List<DownloadFileInfo> downloadFileList = null;
private ManualResetEvent evtDownload = null;
private ManualResetEvent evtPerDonwload = null;
private WebClient clientDownload = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DownloadProgress(List<DownloadFileInfo> downloadFileList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.downloadFileList = downloadFileList;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void OnFormClosing(object sender, FormClosingEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!isFinished && DialogResult.No == MessageBox.Show("当前正在更新,是否取消?", "自动升级", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Cancel = true;
return;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (clientDownload != null)
clientDownload.CancelAsync();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
evtDownload.Set();
evtPerDonwload.Set();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void OnFormLoad(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
evtDownload = new ManualResetEvent(true);
evtDownload.Reset();
Thread t = new Thread(new ThreadStart(ProcDownload));
t.Name = "download";
t.Start();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
long total = 0;
long nDownloadedTotal = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void ProcDownload()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
evtPerDonwload = new ManualResetEvent(false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DownloadFileInfo file in this.downloadFileList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
total += file.Size;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (!evtDownload.WaitOne(0, false))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.downloadFileList.Count == 0)
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DownloadFileInfo file = this.downloadFileList[0];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Debug.WriteLine(String.Format("Start Download:{0}", file.FileName));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.ShowCurrentDownloadFileName(file.FileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//下载
clientDownload = new WebClient();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
clientDownload.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileCompleted);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
evtPerDonwload.Reset();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName + ".tmp"), file);
//等待下载完成
evtPerDonwload.WaitOne();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientDownload.Dispose();
clientDownload = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//移除已下载的文件
this.downloadFileList.Remove(file);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Debug.WriteLine("All Downloaded");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this.downloadFileList.Count == 0)
Exit(true);
else
Exit(false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
evtDownload.Set();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DownloadFileInfo file = e.UserState as DownloadFileInfo;
nDownloadedTotal += file.Size;
this.SetProcessBar(0, (int)(nDownloadedTotal * 100 / total));
//Debug.WriteLine(String.Format("Finish Download:{0}", file.FileName));
//替换现有文件
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName);
if (File.Exists(filePath))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (File.Exists(filePath + ".old"))
File.Delete(filePath + ".old");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
File.Move(filePath, filePath + ".old");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
File.Move(filePath + ".tmp", filePath);
//继续下载其它文件
evtPerDonwload.Set();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.SetProcessBar(e.ProgressPercentage, (int)((nDownloadedTotal + e.BytesReceived) * 100 / total));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delegate void ShowCurrentDownloadFileNameCallBack(string name);
private void ShowCurrentDownloadFileName(string name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.labelCurrentItem.InvokeRequired)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ShowCurrentDownloadFileNameCallBack cb = new ShowCurrentDownloadFileNameCallBack(ShowCurrentDownloadFileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Invoke(cb, new object[]
{ name });
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.labelCurrentItem.Text = name;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delegate void SetProcessBarCallBack(int current, int total);
private void SetProcessBar(int current, int total)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.progressBarCurrent.InvokeRequired)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetProcessBarCallBack cb = new SetProcessBarCallBack(SetProcessBar);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Invoke(cb, new object[]
{ current, total });
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.progressBarCurrent.Value = current;
this.progressBarTotal.Value = total;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delegate void ExitCallBack(bool success);
private void Exit(bool success)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.InvokeRequired)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ExitCallBack cb = new ExitCallBack(Exit);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.Invoke(cb, new object[]
{ success });
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.isFinished = success;
this.DialogResult = success ? DialogResult.OK : DialogResult.Cancel;
this.Close();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void OnCancel(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
evtDownload.Set();
evtPerDonwload.Set();
}
}
}
在构造函数中,将要下载的文件列表传进来
public DownloadProgress(List<DownloadFileInfo> downloadFileList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
InitializeComponent();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.downloadFileList = downloadFileList;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在Form的Load事件中,启动下载线程,开始下载。
private void OnFormLoad(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
evtDownload = new ManualResetEvent(true);
evtDownload.Reset();
Thread t = new Thread(new ThreadStart(ProcDownload));
t.Name = "download";
t.Start();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下载线程没什么特殊的,使用了WebClient的异步下载文件函数DownloadFileAsync,并且注册了两个事件,分别负责下载进度显示和下载完成后的处理:
clientDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
clientDownload.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileCompleted);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
大家看一下就明白了。
private void ProcDownload()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
evtPerDonwload = new ManualResetEvent(false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DownloadFileInfo file in this.downloadFileList)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
total += file.Size;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while (!evtDownload.WaitOne(0, false))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.downloadFileList.Count == 0)
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DownloadFileInfo file = this.downloadFileList[0];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Debug.WriteLine(String.Format("Start Download:{0}", file.FileName));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.ShowCurrentDownloadFileName(file.FileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//下载
clientDownload = new WebClient();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged);
clientDownload.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileCompleted);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
evtPerDonwload.Reset();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName + ".tmp"), file);
//等待下载完成
evtPerDonwload.WaitOne();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
clientDownload.Dispose();
clientDownload = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//移除已下载的文件
this.downloadFileList.Remove(file);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Debug.WriteLine("All Downloaded");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this.downloadFileList.Count == 0)
Exit(true);
else
Exit(false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
evtDownload.Set();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后,在OnDownloadFileCompleted函数中进行最后的处理。包括备份原文件,替换现有文件等。
void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DownloadFileInfo file = e.UserState as DownloadFileInfo;
nDownloadedTotal += file.Size;
this.SetProcessBar(0, (int)(nDownloadedTotal * 100 / total));
//Debug.WriteLine(String.Format("Finish Download:{0}", file.FileName));
//替换现有文件
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName);
if (File.Exists(filePath))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (File.Exists(filePath + ".old"))
File.Delete(filePath + ".old");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
File.Move(filePath, filePath + ".old");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
File.Move(filePath + ".tmp", filePath);
//继续下载其它文件
evtPerDonwload.Set();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
其它的函数只是一些显示进度条和下载信息的,这里就不再详细介绍了。大家可以下载源码看一下。
最后说一下,由于个人能力和时间的问题,这个组件还有一些不完善的地方,比如配置文件比较复杂等,如果您对这个组件有什么改进的话,请务必发给我一份,我的邮件是:
iyondkoo@gmail.com.谢谢。
再做个广告:
VS2005专业教程站是小弟最近做的一个网站,主要是提供VS2005、ASP.NET相关教程,希望大家有什么问题可以去看一下,同时给我提些建议。谢谢大家啦。
源码下载
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
· 用 C# 插值字符串处理器写一个 sscanf