4.2 获取客户端应用程序及服务器端升级程序的最近一次更新日期
通过GetTheLastUpdateTime()函数来实现。
| private string GetTheLastUpdateTime(string Dir) { string LastUpdateTime = ""; string AutoUpdaterFileName = Dir + @"\AutoUpdater.xml"; if(!File.Exists(AutoUpdaterFileName)) return LastUpdateTime; //打开xml文件 FileStream myFile = new FileStream(AutoUpdaterFileName,FileMode.Open); //xml文件阅读器 XmlTextReader xml = new XmlTextReader(myFile); while(xml.Read()) { if(xml.Name == "UpdateTime") { //获取升级文档的最后一次更新日期 LastUpdateTime = xml.GetAttribute("Date"); break; } } xml.Close(); myFile.Close(); return LastUpdateTime; } |
通过XmlTextReader打开XML文档,读取更新时间从而获取Date对应的值,即服务器端升级文件的最近一次更新时间。
函数调用实现:
//获取客户端指定路径下的应用程序最近一次更新时间
string thePreUpdateDate = GetTheLastUpdateTime(Application.StartupPath);
Application.StartupPath指客户端应用程序所在的路径。
//获得从服务器端已下载文档的最近一次更新日期
string theLastsUpdateDate = GetTheLastUpdateTime(theFolder.FullName);
theFolder.FullName指在升级文档下载到客户机上的临时文件夹所在的路径。
4.3 比较日期
客户端应用程序最近一次更新日期与服务器端升级程序的最近一次更新日期进行比较。
| //获得已下载文档的最近一次更新日期 string theLastsUpdateDate = GetTheLastUpdateTime(theFolder.FullName); if(thePreUpdateDate != "") { //如果客户端将升级的应用程序的更新日期大于服务器端升级的应用程序的更新日期 if(Convert.ToDateTime(thePreUpdateDate)>=Convert.ToDateTime(theLastsUpdateDate)) { MessageBox.Show("当前软件已经是最新的,无需更新!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.Close(); } } this.labDownFile.Text = "下载更新文件"; this.labFileName.Refresh(); this.btnCancel.Enabled = true; this.progressBar.Position = 0; this.progressBarTotal.Position = 0; this.progressBarTotal.Refresh(); this.progressBar.Refresh(); //通过动态数组获取下载文件的列表 ArrayList List = GetDownFileList(GetTheUpdateURL(),theFolder.FullName); string[] urls = new string[List.Count]; List.CopyTo(urls, 0); |
将客户端升级的应用程序的日期与服务器端下载的应用程序日期进行比较,如果前者大于后者,则不更新;如果前者小于后者,则通过动态数组获取下载文件的列表,开始下载文件。

