static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { //an exception happened. ignore the update } else { UpdaterInfo info = e.Result as UpdaterInfo; if (info.result != info.currentVersion) { string msg = "An update (from " + info.currentVersion + " to " + info.result + ") of " + info.productName + " is available online. Do you want to install the update?"; MessageBoxResult result = MessageBox.Show(msg, "Updater", MessageBoxButton.YesNo, MessageBoxImage.Information); if (result == MessageBoxResult.Yes) { string installer = baseSiteAddress + info.installerFile; try { Process.Start("explorer.exe", installer); info.callback(); } catch (Exception) { } } } } }
static void worker_DoWork(object sender, DoWorkEventArgs e) { UpdaterInfo info = e.Argument as UpdaterInfo; string url = baseSiteAddress + info.versionFile; string availableVersion = string.Empty; using (XmlReader reader = XmlReader.Create(url)) { reader.ReadToFollowing("Version"); availableVersion = reader.GetAttribute("Available"); reader.Close(); } info.result = availableVersion; e.Result = info; }
public static void DoUpdate(string productName, string currentVersion, string versionFile, string installerFile, UpdateFiredDelegate callback) { BackgroundWorker worker = new BackgroundWorker(); worker.WorkerReportsProgress = false; worker.WorkerSupportsCancellation = false; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); UpdaterInfo info = new UpdaterInfo(); info.productName = productName; info.currentVersion = currentVersion; info.versionFile = versionFile; info.installerFile = installerFile; info.callback = callback; worker.RunWorkerAsync(info); }
/// <summary> /// Fetches updater information from the remote update service. /// </summary> /// <returns></returns> public UpdaterInfo FetchUpdaterInfo() { // Create HTTP POST request to the predefined check-in URL HttpWebRequest request = HttpWebRequest.CreateHttp(Configuration.CHECKIN_URI); request.Accept = "application/xml"; request.Method = "GET"; // Get the response stream and check the status code and content type HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { // Something went wrong, server did not send 200 OK throw new InvalidOperationException("Server did not respond with 200 OK"); } // Deserialize response stream from the XML into CheckinResult DataContractSerializer xmlSerializer = new DataContractSerializer(typeof(UpdaterInfo)); UpdaterInfo result = (UpdaterInfo)xmlSerializer.ReadObject(response.GetResponseStream()); return(result); }