public void Update() { if (!config.Enabled) { return; } if (ConstFile.AppPid != 0) { CommonUnitity.KillProcess(ConstFile.AppPid); } bool flag = CommonUnitity.isRunProcessName(ConstFile.AppExe); if (flag) { DialogResult dr = MessageBox.Show(dc, string.Format("进程{0}正在运行,请确定是否要关闭!", ConstFile.AppExe), "自动升级", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dr == DialogResult.Yes) { CommonUnitity.KillProcess(ConstFile.AppExe); } else { CommonUnitity.RestartApplication(); return; } } string urls = ""; if (config.Type == 0) { using (var webClient = new HttpWebClient()) { try { urls = webClient.DownloadString(config.ServerUrl); } catch { try { urls = webClient.DownloadString(config.ServerUrl); } catch (Exception ex) { LogHelper.Error(ex); } } } } else if (config.Type == 1) { urls = config.ServerUrl; } string[] strs = urls.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (strs.Length > 0) { RemoteConfig remoteConfig = null; int count = 2; int retryCount = 0; do { foreach (var str in strs) { try { remoteConfig = ParseRemoteXml(str); if (remoteConfig != null) { break; } } catch (Exception ex) { LogHelper.Error(str + ":" + ex.Message); } } retryCount++; } while (remoteConfig == null && retryCount < count); if (remoteConfig != null) { string version = remoteConfig.Version; string mversion = remoteConfig.MinimumRequiredVersion; LogHelper.Info(string.Format("version:{0},mversion:{1}", version, mversion)); LogHelper.Info(string.Format("comment:{0}", remoteConfig.Comment)); try { Version v = new Version(version); config.Version = v.ToString(); } catch (Exception ex) { LogHelper.Error(ex); } Dictionary <string, RemoteFile> listRemotFile = remoteConfig.RemoteFiles; List <DownloadFileInfo> downloadList = new List <DownloadFileInfo>(); foreach (LocalFile file in config.UpdateFileList) { if (listRemotFile.ContainsKey(file.Path)) { try { RemoteFile rf = listRemotFile[file.Path]; Version v1 = new Version(rf.LastVer); Version v2 = new Version(file.LastVer); if (v1 > v2 || rf.Size != file.Size || rf.MD5 != file.MD5) { downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size)); file.LastVer = rf.LastVer; file.Size = rf.Size; file.MD5 = rf.MD5; if (rf.NeedRestart) { bNeedRestart = true; } bDownload = true; } listRemotFile.Remove(file.Path); } catch (Exception ex) { LogHelper.Error(ex); //EventLog.WriteEntry("AutoUpdater Update", ex.StackTrace, EventLogEntryType.Error); } } } foreach (RemoteFile file in listRemotFile.Values) { downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size)); LocalFile lf = new LocalFile(file.Path, file.LastVer, file.Size, file.MD5); config.UpdateFileList.Add(lf); if (file.NeedRestart) { bNeedRestart = true; } bDownload = true; } downloadFileListTemp = new List <DownloadFileInfo>(); foreach (var downloadFileInfo in downloadList) { downloadFileListTemp.Add(downloadFileInfo); } if (bDownload) { dc.downloadFileList = downloadList; //dc.IsCanCancel = isCanCancel; dc.IsCanCancel = true; if (this.OnShow != null) { this.OnShow(); } if (DialogResult.OK == dc.ShowDialog()) { StartDownload(downloadList); } else { CommonUnitity.RestartApplication(); } } } else { LogHelper.Error("remoteConfig is null"); CommonUnitity.ShowErrorAndRestartApplication( string.Format("{0}更新不成功[无法获取升级清单文件,请检查网络是否正常],请单击退出程序", ConstFile.AppName), false); } } else { LogHelper.Error("url is null"); CommonUnitity.ShowErrorAndRestartApplication( string.Format("{0}更新不成功[无法连接到升级服务器,请检查网络是否正常],请单击退出程序", ConstFile.AppName), false); } }
private void ProcDownload(object o) { string tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME); if (!Directory.Exists(tempFolderPath)) { Directory.CreateDirectory(tempFolderPath); } evtPerDonwload = new ManualResetEvent(false); foreach (DownloadFileInfo file in this.downloadFileList) { total += file.Size; } try { while (!evtDownload.WaitOne(0, false) && !bCancel) { if (this.downloadFileList.Count == 0) { break; } this.SetProcessBar(100, (int)(nDownloadedTotal * 100 / total)); DownloadFileInfo file = this.downloadFileList[0]; LogHelper.Debug(string.Format("Start Download:{0}", file.FileName)); //Debug.WriteLine(String.Format("Start Download:{0}", file.FileName)); this.ShowCurrentDownloadFileName(file.FileName); //Download using (clientDownload = new HttpWebClient()) { //Added the function to support proxy clientDownload.Proxy = null; //clientDownload.Proxy.Credentials = CredentialCache.DefaultCredentials; //clientDownload.Credentials = System.Net.CredentialCache.DefaultCredentials; //End added clientDownload.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => { try { this.SetProcessBar(e.ProgressPercentage, (int) ((nDownloadedTotal + e.BytesReceived) * 100 / total)); } catch (Exception ex) { LogHelper.Error(ex); //EventLog.WriteEntry("DownloadProgress", ex.Message, // EventLogEntryType.Error); } }; clientDownload.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) => { try { //DealWithDownloadErrors(); DownloadFileInfo dfile = e.UserState as DownloadFileInfo; try { string newPath = Path.Combine( tempFolderPath, dfile.FileFullName); FileInfo f = new FileInfo(newPath); if (dfile.Size != f.Length) { LogHelper.Error( string.Format( "{0}.Size({1})!={2}.Size({3})", dfile.FileName, dfile.Size, f.Name, f.Length)); LogHelper.Error(e.Error); isRetry = true; retryCount++; } else { isRetry = false; retryCount = 0; LogHelper.Debug( string.Format( "Download {0} Success", dfile.FileName)); } } catch (Exception ex) { isRetry = true; retryCount++; LogHelper.Debug( string.Format( "Download {0} Failed", dfile.FileName)); LogHelper.Error(ex); } if (!isRetry) { nDownloadedTotal += dfile.Size; this.SetProcessBar(100, (int) (nDownloadedTotal * 100 / total)); } evtPerDonwload.Set(); } catch (Exception ex) { isRetry = true; retryCount++; LogHelper.Error(ex); } }; evtPerDonwload.Reset(); //Download the folder file string tempFolderPath1 = CommonUnitity.GetFolderUrl(file); if (!string.IsNullOrEmpty(tempFolderPath1)) { tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME); tempFolderPath += tempFolderPath1; } else { tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstFile.TEMPFOLDERNAME); } clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), Path.Combine(tempFolderPath, file.FileFullName), file); //Wait for the download complete evtPerDonwload.WaitOne(); if (isRetry) { LogHelper.Debug(string.Format("{0} retry Download[retry count:{1}]", file.FileName, retryCount)); } if (!isRetry) { this.downloadFileList.Remove(file); } else if (retryCount >= 5) { LogHelper.Debug("Retry Timeout"); bCancel = true; } } } System.GC.Collect(); if (bCancel) { if (retryCount == 5) { CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}更新不成功[网络异常,请确保网络环境稳定],{0}现在将重新启动,尝试再次更新,请单击确定重新启动程序!", ConstFile.AppName, ""), true); } CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}更新不成功[{1}],{0}现在将重新启动,尝试再次更新,请单击确定重新启动程序!", ConstFile.AppName, "用户取消升级"), true); } } catch (Exception ex) { //EventLog.WriteEntry("DownloadProgress", ex.Message, LogHelper.Error(ex); //EventLogEntryType.Error); CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}[{1}]", ConstFile.NOTNETWORK, ex.Message), true); //throw; } //When the files have not downloaded,return. if (downloadFileList.Count > 0) { return; } foreach (DownloadFileInfo file in this.allFileList) { string tempUrlPath = CommonUnitity.GetFolderUrl(file); string oldPath = string.Empty; string newPath = string.Empty; if (!string.IsNullOrEmpty(tempUrlPath)) { oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME + tempUrlPath, file.FileName); } else { oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME, file.FileName); } } LogHelper.Debug("All Downloaded"); try { foreach (DownloadFileInfo file in this.allFileList) { string tempUrlPath = CommonUnitity.GetFolderUrl(file); string oldPath = string.Empty; string newPath = string.Empty; if (!string.IsNullOrEmpty(tempUrlPath)) { oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME + tempUrlPath, file.FileName); } else { oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstFile.TEMPFOLDERNAME, file.FileName); } //Added for dealing with the config file download errors string newfilepath = string.Empty; if (newPath.Substring(newPath.LastIndexOf(".") + 1).Equals(ConstFile.CONFIGFILEKEY)) { if (System.IO.File.Exists(newPath)) { if (newPath.EndsWith("_")) { newfilepath = newPath; newPath = newPath.Substring(0, newPath.Length - 1); oldPath = oldPath.Substring(0, oldPath.Length - 1); } File.Move(newfilepath, newPath); } } //End added if (File.Exists(oldPath)) { MoveFolderToOld(oldPath, newPath); } else { //Edit for config_ file if (!string.IsNullOrEmpty(tempUrlPath)) { if (!Directory.Exists(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1))) { Directory.CreateDirectory(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1)); MoveFolderToOld(oldPath, newPath); } else { MoveFolderToOld(oldPath, newPath); } } else { MoveFolderToOld(oldPath, newPath); } } } } catch (Exception ex) { LogHelper.Error(ex); CommonUnitity.ShowErrorAndRestartApplication(string.Format("{0}更新不成功[未知异常,请重启电脑或手动删除程序目录下面后缀为.old的所有文件],请单击退出程序", ConstFile.AppName), false); } finally { this.allFileList.Clear(); if (this.downloadFileList.Count == 0) { Exit(true); } else { Exit(false); } evtDownload.Set(); } }