在下载服务器上的 Update.xml 文件时,不需要用 WebClient.DownloadFileAsync 异步下载,因为 Update.xml 配置文件往往很小,没有必要再来显示下进度。
//全局变量定义
WebClient client = new WebClient(); //使用WebClient下载
int downfilenum = 0; int downlistnum = 0;
ArrayList downlist = new ArrayList();
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{ //下载过程中
int a = e.ProgressPercentage;
progressBar1.Value = a;
label1.Text = a + "% Downloading... ";
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{ //下载完成时
downfilenum++;
if (client != null) { client.CancelAsync(); }
if (downfilenum < downlistnum) DownLoadFile(downlist[downfilenum].ToString()); //下载剩余的
if (downfilenum == downlistnum) { init2(); } //继续初始化
}
//获取本地的版本信息
private string GetTheLastUpdateTime(string Dir)
{省略}
//获取临时文件里的下载文件地址
private ArrayList GetDownFileList(string Dir)
{省略}
//HTTP下载远程文件并保存本地的函数
private void DownLoad(string downAddress, string savePath, Boolean Async)
{
DirectoryInfo di = Directory.GetParent(savePath);
if (!di.Exists) di.Create();
WebClient client = new WebClient(); //再次new 避免WebClient不能I/O并发
if (Async)
{ //异步下载
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(downAddress), savePath);
}
else client.DownloadFile(new Uri(downAddress), savePath);
}
//下载某个文件
private void DownLoadFile(string url)
{
string curdir = Application.StartupPath;
string[] arr = url.Split('/');
string filename = arr[arr.Count() - 1];
label2.Text = filename;
DownLoad(url, curdir + @"\temp\" + filename, true); //异步下载远程文件
}
//初始化
private void init()
{
string curdir = Application.StartupPath;
DownLoad("http://www.piaoyi.org/tool/ppproxy/Update.xml", curdir + @"\temp\Update.xml", false); //非异步下载远程文件
string thePreUpdateDate = GetTheLastUpdateTime(curdir); //本地版本信息
string theLastsUpdateDate = GetTheLastUpdateTime(curdir + @"\temp\"); //远程下载本地后的版本信息
if (thePreUpdateDate == "") thePreUpdateDate = "1990-10-10";
//比较应用程序的更新日期
if (Convert.ToDateTime(thePreUpdateDate) >= Convert.ToDateTime(theLastsUpdateDate))
{
MessageBox.Show("当前软件已经是最新的,欢迎使用!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (Directory.Exists(curdir + @"\temp\")) Directory.Delete(curdir + @"\temp\", true); //删除临时目录
this.Close();
}
else
{
MessageBox.Show("发现新版本,软件即将自动升级...", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//关闭原有应用程序的所有进程
System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("PpProxy");
foreach (System.Diagnostics.Process pro in proc)
{
pro.Kill();
}
downlist = GetDownFileList(curdir + @"\temp"); //获取需要下载的文件列表
downlistnum = downlist.Count;
if (downlistnum > 0) DownLoadFile(downlist[0].ToString()); //异步下载第一个
}
}
private void init2()
{
string curdir = Application.StartupPath;
DirectoryInfo theFolder = new DirectoryInfo(curdir + @"\temp");
if (theFolder.Exists)
{
foreach (FileInfo theFile in theFolder.GetFiles())
{
//如果临时文件夹下存在与应用程序所在目录下的文件同名的文件,则删除应用程序目录下的文件
if (File.Exists(curdir + "\\" + Path.GetFileName(theFile.FullName)))
File.Delete(curdir + "\\" + Path.GetFileName(theFile.FullName));
//将临时文件夹的文件移到应用程序所在的目录下
File.Move(theFile.FullName, curdir + "\\" + Path.GetFileName(theFile.FullName));
}
}
if (Directory.Exists(curdir + @"\temp\")) Directory.Delete(curdir + @"\temp\", true); //删除临时目录
//启动安装程序
this.label1.Text = "正在启动程序....";
System.Diagnostics.Process.Start(curdir + @"\PpProxy.exe");
this.Close();
}
//
private void Form1_Load(object sender, EventArgs e)
{
init(); //初始化
}