void UpdateOtherAPP() { download = new DownloadFiles(); ChangeControlText("正在讀取更新包資訊...", label3); {//下載到temp\APP.ini HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ini_AutoUpdater.IniReadValue("AutoUpdater_URL", "APP_Info_ini", 200)); HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); System.IO.Stream dataStream = httpResponse.GetResponseStream(); byte[] buffer = new byte[8192]; FileStream fs = new FileStream(Path.Combine(Path.GetTempPath(), ini_AutoUpdater.IniReadValue("APP_Info", "APP_Name", 200) + ".ini"), FileMode.Create, FileAccess.Write); int size = 0; do { if (download.GetStatus().StopNow) { break; } size = dataStream.Read(buffer, 0, buffer.Length); if (size > 0) { fs.Write(buffer, 0, size); } } while (size > 0); fs.Close(); httpResponse.Close(); } //載入APP.ini到updateinfo updateinfo = new UpdateInfo(new IniFile(Path.Combine(Path.GetTempPath(), ini_AutoUpdater.IniReadValue("APP_Info", "APP_Name", 200) + ".ini"))); updateinfo.LodeIni(); Version Version_localFile = new Version(), Version_UpdateFile = new Version(); bool au = false; if (CheckFile.GetVersion(updateinfo.APP_FileName) != "Error") { Version_localFile = new Version(CheckFile.GetVersion(updateinfo.APP_FileName)); } else { //ChangeControlText("找不到\"" + updateinfo.APP_FileName+"\"", label3); au = true; // return; } try { Version_UpdateFile = new Version(updateinfo.APP_Version); } catch { ChangeControlText("APP_Version有問題", label3); } if (download.GetStatus().StopNow) { return; } int tm; if (!au) { tm = Version_UpdateFile.CompareTo(Version_localFile); } else//永遠更新 { tm = 100; } if (tm <= 0) { //沒有更新 if (WithForm) { FormClose(this); return; } else { MessageBox.Show("無須更新"); return; } } else {//發現更新 ChangeControlText(String.Format("發現新版本({0}), 現行版本 {1}", updateinfo.APP_Version, Version_localFile.ToString()), label3); if (updateinfo.Update_DoNotAsk) { UsePackUpdate(); } else { if (MessageBox.Show(String.Format("現行版本:{0}\r\n最新版本:{1}\r\nChangeLog:\r\n {2}", Version_localFile.ToString(), updateinfo.APP_Version, updateinfo.APP_ChangeLog), "是否立即更新?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.No) {//否 FormClose(this); } else {//立即更新 UsePackUpdate(); } } } }
private void timer2_Tick(object sender, EventArgs e) { temp = download.GetStatus(); progressBar1.Value = (int)(temp.Progress * 100); label1.Text = (temp.TOTALSIZE / 1024).ToString("0.00") + "KB /" + (temp.MAXSIZE / 1024).ToString("0.00") + "KB"; if (temp.Isdownloading == false) { timer2.Stop(); if (CheckFile.GetMD5(updateinfo.ZIP_Name).ToUpper() == updateinfo.ZIP_MD5.ToUpper()) { CloseAPP(); RunAPP(1); if (System.IO.Directory.Exists(Path.Combine(Application.StartupPath, "temp"))) { System.IO.Directory.Delete(Path.Combine(Application.StartupPath, "temp"), true); } if (updateinfo.ZIP_Password == "") { UseZip.UnZip(updateinfo.ZIP_Name, Path.Combine(Application.StartupPath, "temp")); } else { UseZip.UnZip(updateinfo.ZIP_Name, updateinfo.ZIP_Password, Path.Combine(Application.StartupPath, "temp")); } foreach (string fname in System.IO.Directory.GetFiles(Path.Combine(Application.StartupPath, "temp"), "*.*", SearchOption.AllDirectories)) { foreach (File_UpdateInfo tt in updateinfo.File_UpdateMod) { switch (tt.Mod) { case "WhenNeed": if (tt.FileName == (fname.IndexOf("temp\\") == 0 ? fname.Substring(5, fname.Length - 5) : fname)) { if (CheckFile.GetMD5(tt.FileName) != CheckFile.GetMD5(fname)) { File.Copy(fname, Path.Combine(Directory.GetParent(Path.GetDirectoryName(fname)).FullName, Path.GetFileName(fname)), true); } } break; case "Always": File.Copy(fname, Path.Combine(Directory.GetParent(Path.GetDirectoryName(fname)).FullName, Path.GetFileName(fname)), true); break; case "WhenNotExist": File.Copy(fname, Path.Combine(Directory.GetParent(Path.GetDirectoryName(fname)).FullName, Path.GetFileName(fname)), false); break; case "Ignore": break; default: File.Copy(fname, Path.Combine(Directory.GetParent(Path.GetDirectoryName(fname)).FullName, Path.GetFileName(fname)), true); break; } } } if (System.IO.Directory.Exists(Path.Combine(Application.StartupPath, "temp"))) { System.IO.Directory.Delete(Path.Combine(Application.StartupPath, "temp"), true); } if (File.Exists(updateinfo.ZIP_Name)) { File.Delete(updateinfo.ZIP_Name); } RunAPP(2); ChangeControlText("更新完成!", label3); if (!WithForm) { MessageBox.Show("更新完成!"); FormClose(this); } Recheck.Enabled = true; } else { label3.Text = ("檔案驗證失敗,請再試一次"); Recheck.Enabled = true; } } }