/// 需要在某個時機呼叫這個,一般來說是在 MainForm Load 的事件中。 /// </summary> internal static void Update() { string basePath = System.Windows.Forms.Application.StartupPath; //更新位置。 string appUrl = "http://module.ischool.com.tw/module/89/MOD_ischedule/app.xml"; if (System.IO.File.Exists(Path.Combine(System.Windows.Forms.Application.StartupPath, "測試版本"))) appUrl = "http://10.1.1.167/modules/FiscaAEModules/MOD_ischedule/app.xml"; string script_path = Path.Combine(basePath, "update_padding.xml"); ManifestResolver resolver = new ManifestResolver(appUrl, VersionOption.Stable); resolver.VerifySignature = true; UpdateHelper uc = new UpdateHelper(); uc.Resolver = resolver; uc.Install = new InstallDescriptor(basePath); //更新過程的訊息。 resolver.ProgressMessage += new EventHandler<ProgressMessageEventArgs>(au_ProgressMessage); uc.DownloadStarted += new EventHandler(au_DownloadStarted); uc.DownloadProgressChanged += new EventHandler<DownloadProgressEventHandler>(au_DownloadProgressChanged); uc.DownloadCompleted += new EventHandler(au_DownloadCompleted); uc.ProgressMessage += new EventHandler<ProgressMessageEventArgs>(au_ProgressMessage); //MainForm.SetBarMessage("檢查更新…"); ThreadPool.QueueUserWorkItem(arg => { resolver.Resolve(); if (uc.CheckUpdate()) { PaddingScript script = new PaddingScript(); script.WaitRelease(System.Reflection.Assembly.GetExecutingAssembly().Location); uc.Update(script); script.Delete(uc.Install.TemporalFolder); script.Delete(script_path); script.DeleteEmpty(basePath); script.StartProcess(System.Reflection.Assembly.GetExecutingAssembly().Location); script.Save(script_path); MessageBox.Show("系統自動更新完成,將重新啟動。"); Application.Restart(); } else { //MessageBox.Show("已經是最新版。"); } }); }
void timer_Tick(object state) { try { if (_updateHelper.CheckUpdate()) { PaddingScript script = new PaddingScript(); script.WaitRelease(Application.ExecutablePath); _updateHelper.Update(script); script.Delete(Path.Combine(Application.StartupPath, "_update_padding.xml")); script.Delete(Path.Combine(Application.StartupPath, _updateHelper.Install.TemporalFolder)); script.StartProcess(Application.ExecutablePath); script.Save(Path.Combine(Application.StartupPath, "_update_padding.xml")); Invoke(new Action(() => { tsVersionLabel.Visible = true; })); } } catch { } }