// this method is called when the checkForUpdate finishes checking // for the new version. If this method returns true, our checkForUpdate // object will download the installer public bool OnCheckForUpdateFinished(DownloadedVersionInfo versionInfo) { if ((versionInfo.error) || (versionInfo.installerUrl.Length == 0) || (versionInfo.latestVersion == null)) { MessageBox.Show(this, "Đã có lỗi xãy ra trong quá trình kiểm tra phiên bản mới", "Kiểm tra cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } // compare the current version with the downloaded version number Version curVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; if (curVer.CompareTo(versionInfo.latestVersion) >= 0) { // no new version //MessageBox.Show(this, "No new version detected", "Check for updates", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(false); } // new version found, ask the user if he wants to download the installer string str = String.Format("Đã có phiên bản mới của phần mềm!\nPhiên bản bạn đang dùng: {0}.\nPhiên bản mới nhất: {1}.", curVer, versionInfo.latestVersion); //return DialogResult.Yes == MessageBox.Show(this, str, "Kiểm tra cập nhật", MessageBoxButtons.YesNo, MessageBoxIcon.Question); return(true); }
// 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 }); }