public MainForm(string settingsXMLPath, int processID) { InitializeComponent(); this.businessLogic = new UpdaterBusiness(); if (settingsXMLPath.Length < 1 || System.IO.File.Exists(settingsXMLPath)) { this.updaterSettings = this.businessLogic.LoadSettings(settingsXMLPath); } this.processID = processID; }
/// <summary> /// Updates the specified updater settings. /// </summary> /// <param name="updaterSettings">The updater settings.</param> /// <returns>the result of the update</returns> public EUpdateResult Update(Settings updaterSettings, int processID) { EUpdateResult updateResult; // TODO get admin rights try { MemoryStream dataStream = null; updateResult = this.CheckUpdatePossible(updaterSettings, out dataStream); if (updateResult == EUpdateResult.None) { System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID); process.Kill(); try { this.BackUpOldData(); updateResult = this.UpdateDatabase(); updateResult = this.UpdateFileData(updaterSettings, dataStream); } catch { updateResult = EUpdateResult.Error; } if (updateResult != EUpdateResult.Success) { this.RestoreBackup(); } // TODO Save Settings with the new version System.Diagnostics.Process.Start(process.MainModule.FileName); } } catch (Exception e) { updateResult = EUpdateResult.Error; // TODO Error schreiben } return updateResult; }
private bool AreSettingsValid(Settings updaterSettings, out Version currentVersion) { if (!Version.TryParse(updaterSettings.CurrentVersion, out currentVersion)) { return false; } string websiteRegEx = "^http(s)?://([\\w-]+.)+[\\w-]+(/[\\w- ./?%&=])?$"; Regex testingSettings = new Regex(websiteRegEx); if (!testingSettings.IsMatch(updaterSettings.WebUpdateFilePath) || !testingSettings.IsMatch(updaterSettings.WebUpdateVersionPath)) { return false; } if (updaterSettings.TempUpdateFilePath.Length < 1 || updaterSettings.ApplicationPath.Length < 1 || System.IO.Directory.Exists(updaterSettings.ApplicationPath) || System.IO.Directory.Exists(updaterSettings.TempUpdateFilePath)) { return false; } return true; }
private EUpdateResult UpdateFileData(Settings updaterSettings, MemoryStream dataStream) { this.DeleteFiles(updaterSettings.ApplicationPath); FastZip fastZip = new FastZip(); fastZip.ExtractZip(dataStream, updaterSettings.ApplicationPath, FastZip.Overwrite.Always, null, string.Empty, string.Empty, false, true); return EUpdateResult.Success; }
private EUpdateResult DownloadDataFiles(Settings updaterSettings, out MemoryStream dataStream) { byte[] data = null; try { data = new System.Net.WebClient().DownloadData(updaterSettings.WebUpdateFilePath + updaterSettings.WebUpdateFileName); } catch { dataStream = null; return EUpdateResult.WebDatasCantDownload; } dataStream = new MemoryStream(data); return EUpdateResult.None; }
private EUpdateResult CheckUpdatePossible(Settings updaterSettings, out MemoryStream dataStream) { EUpdateResult updateResult = EUpdateResult.None; Version currentVersion; if (!this.AreSettingsValid(updaterSettings, out currentVersion)) { dataStream = null; return EUpdateResult.SettingsAreIncorect; } updateResult = this.IsNewVersionAvailable(updaterSettings.WebUpdateVersionPath, currentVersion); if (updateResult != EUpdateResult.None) { dataStream = null; return updateResult; } updateResult = this.DownloadDataFiles(updaterSettings, out dataStream); if (updateResult != EUpdateResult.None) { return updateResult; } return EUpdateResult.None; }