public void CheckUpdate() { var localFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstFile.CHECKUPDATEFILE); var remoteFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,ConstFile.TEMPFOLDERNAME, ConstFile.CHECKUPDATEFILE); var localFileMD5 = ByteStringUtil.ByteArrayToHexStr(HashDigest.FileDigest(localFileName)); using (WebClient client = new WebClient()) { client.DownloadFile(new Uri(config.ServerUrl.Replace(ConstFile.FILENAME, ConstFile.CHECKUPDATEFILE)), remoteFileName); } var remoteFileMD5 = ByteStringUtil.ByteArrayToHexStr(HashDigest.FileDigest(remoteFileName)); if (!localFileMD5.Equals(remoteFileMD5)) { var dc = new DownloadConfirm(config.AppName); if (dc.ShowDialog() == DialogResult.OK) { Update(); File.Copy(remoteFileName, localFileName, true); } } }
public void CheckUpdate() { var localFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstFile.CHECKUPDATEFILE); var remoteFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstFile.TEMPFOLDERNAME, ConstFile.CHECKUPDATEFILE); var localFileMD5 = ByteStringUtil.ByteArrayToHexStr(HashDigest.FileDigest(localFileName)); using (WebClient client = new WebClient()) { client.DownloadFile(new Uri(config.ServerUrl.Replace(ConstFile.FILENAME, ConstFile.CHECKUPDATEFILE)), remoteFileName); } var remoteFileMD5 = ByteStringUtil.ByteArrayToHexStr(HashDigest.FileDigest(remoteFileName)); if (!localFileMD5.Equals(remoteFileMD5)) { var dc = new DownloadConfirm(config.AppName); if (dc.ShowDialog() == DialogResult.OK) { Update(); File.Copy(remoteFileName, localFileName, true); } } }
/// <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() { if (!config.Enabled) { return; } /* * 请求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); Dictionary <string, RemoteFile> listRemotFile = ParseRemoteXml(strXml); List <DownloadFileInfo> downloadList = new List <DownloadFileInfo>(); //某些文件不再需要了,删除 List <LocalFile> preDeleteFile = new List <LocalFile>(); foreach (LocalFile file in config.UpdateFileList) { if (listRemotFile.ContainsKey(file.Path)) { RemoteFile rf = listRemotFile[file.Path]; if (rf.LastVer != file.LastVer) { downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size)); file.LastVer = rf.LastVer; file.Size = rf.Size; if (rf.NeedRestart) { bNeedRestart = true; } } listRemotFile.Remove(file.Path); } else { preDeleteFile.Add(file); } } foreach (RemoteFile file in listRemotFile.Values) { downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size)); config.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size)); if (file.NeedRestart) { bNeedRestart = true; } } if (downloadList.Count > 0) { DownloadConfirm dc = new DownloadConfirm(downloadList); if (this.OnShow != null) { this.OnShow(); } if (DialogResult.OK == dc.ShowDialog()) { foreach (LocalFile file in preDeleteFile) { string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.Path); if (File.Exists(filePath)) { File.Delete(filePath); } config.UpdateFileList.Remove(file); } StartDownload(downloadList); } } }