// called after the checkForUpdate object downloaded the installer public void OnDownloadInstallerinished(DownloadInstallerInfo info) { if (info.error) { MessageBox.Show(this, "Đã có lỗi xãy ra trong quá trình tải phiên bản mới của phần mềm", "Kiểm tra cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } /* * // ask the user if he want to start the installer * if (DialogResult.Yes != MessageBox.Show(this, "Bạn có muốn cài đặt phiên bản mới không?", "Kiểm tra cập nhật", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) * { * // it not - remove the downloaded file * try * { * File.Delete(info.path); * } * catch { } * return; * } */ // run the installer and exit the app try { //Process.Start(info.path); ProcessStart(); //Process.Start(@"AutoUpdater\Updater.exe"); this.Close(); } catch (Exception) { MessageBox.Show(this, "Error while running the installer.", "Check for updates", MessageBoxButtons.OK, MessageBoxIcon.Error); try { File.Delete(info.path); } catch { } return; } return; }
// this is run in a thread. do the whole updating process: // - check for the new version (downloading the xml file) // - download the installer // the communication with the Form is done with the delegates private void CheckForUpdateFunction() { DownloadedVersionInfo i = new DownloadedVersionInfo(); i.error = true; i.installerUrl = ""; i.homeUrl = ""; try { XmlTextReader reader = new XmlTextReader(xmlFileUrl); reader.MoveToContent(); string elementName = ""; Version newVer = null; string url = ""; string msiUrl = ""; if (StopWorkerThread()) { return; } if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "appinfo")) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { elementName = reader.Name; } else { if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue)) { switch (elementName) { case "version": newVer = new Version(reader.Value); break; case "url": url = reader.Value; break; case "installer": msiUrl = reader.Value; break; case "date": //elementName = elementName; break; } } } } } reader.Close(); i.error = false; i.latestVersion = newVer; i.homeUrl = url; i.installerUrl = msiUrl; } catch (Exception) { } if (StopWorkerThread()) { return; } bool download = (bool)this.mainApp.Invoke(new DelegateCheckForUpdateFinished(mainApp.OnCheckForUpdateFinished), new Object[] { i }); if (!download) { return; } // download and let the main thread know DownloadInstallerInfo i2 = new DownloadInstallerInfo(); i2.error = true; string filepath = ""; try { WebRequest request = WebRequest.Create(i.installerUrl); WebResponse response = request.GetResponse(); string filename = ""; int contentLength = 0; for (int a = 0; a < response.Headers.Count; a++) { try { string val = response.Headers.Get(a); switch (response.Headers.GetKey(a).ToLower()) { case "content-length": contentLength = Convert.ToInt32(val); break; case "content-disposition": string[] v2 = val.Split(';'); foreach (string s2 in v2) { string[] v3 = s2.Split('='); if (v3.Length == 2) { if (v3[0].Trim().ToLower() == "filename") { char[] sss = { ' ', '"' }; filename = v3[1].Trim(sss); } } } break; } } catch (Exception) { }; } if (StopWorkerThread()) { return; } if (filename.Length == 0) { filename = "installer.zip"; } //string updaterPath = Path.Combine(Directory.GetCurrentDirectory(), "AutoUpdater"); //filepath = Path.Combine(updaterPath, filename); filepath = Path.Combine(Directory.GetCurrentDirectory(), filename); if (File.Exists(filepath)) { try { File.Delete(filepath); } catch { } if (File.Exists(filepath)) { string rname = Path.GetRandomFileName(); rname.Replace('.', '_'); rname += ".zip"; filepath = Path.Combine(Directory.GetCurrentDirectory(), rname); //filepath = Path.Combine(updaterPath, rname); } } Stream stream = response.GetResponseStream(); int pos = 0; byte[] buf2 = new byte[8192]; FileStream fs = new FileStream(filepath, FileMode.CreateNew); while ((0 == contentLength) || (pos < contentLength)) { int maxBytes = 8192; if ((0 != contentLength) && ((pos + maxBytes) > contentLength)) { maxBytes = contentLength - pos; } int bytesRead = stream.Read(buf2, 0, maxBytes); if (bytesRead <= 0) { break; } fs.Write(buf2, 0, bytesRead); if (StopWorkerThread()) { return; } pos += bytesRead; } fs.Close(); stream.Close(); i2.error = false; i2.path = filepath; } catch { // when something goes wrong - at least do the cleanup :) if (filepath.Length > 0) { try { File.Delete(filepath); } catch { } } } if (StopWorkerThread()) { return; } this.mainApp.BeginInvoke(new DelegateDownloadInstallerFinished(mainApp.OnDownloadInstallerinished), new Object[] { i2 }); }