public Update(string versionS, string zipfileUrl) { string filename = zipfileUrl.Substring(zipfileUrl.LastIndexOf('/') + 1); string zipfilePath = Application.StartupPath + "\\" + filename; string sDir = Application.StartupPath; //////////////////////////////////////////////// //step2. download //////////////////////////////////////////////// try{ WebClient _wc = new WebClient(); log("download now..." + zipfileUrl); _wc.DownloadFile(zipfileUrl, zipfilePath);//download zip file log("download over..." + zipfilePath); //////////////////////////////////////////////// //step3. kill & unzip //////////////////////////////////////////////// log("kill process of ruehelper.exe now..."); KillProcess(ApplicationName); { Thread.Sleep(1000); log("unzip now..."); ZipClass.UnZip(zipfilePath, "", 1, null); File.Delete(zipfilePath); } //////////////////////////////////////////////// //step4. replace //////////////////////////////////////////////// log("set update.version=" + versionS); IniClass iniC = new IniClass(Application.StartupPath + @"\Update.ini"); iniC.IniWriteValue("update", "version", versionS);//更新成功后将版本写入配置文件 //////////////////////////////////////////////// //step5. run //////////////////////////////////////////////// log("restart RueHelper.exe now..."); Application.Exit();//退出升级程序 log("Process.Start(" + sDir + "\\" + ApplicationName + ") now..."); } catch (Exception e) { log("update error, exit!!! " + e.Message); return; } try { Process.Start(sDir + "\\" + ApplicationName);//打开主程序Main.exe } catch (Exception e) { log("Process.Start exception. " + e.Message); } }
/// <summary> /// 下载完成后触发 /// </summary> void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { zipsIndex++; if (zipsIndex < zips.Length) { //继续下载下一个压缩包 wc = new WebClient(); wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted); wc.DownloadFileAsync(new Uri(url + zips[zipsIndex]), zips[zipsIndex]); } else { //解压 int maximum = ZipClass.GetMaximum(zips); foreach (string zip in zips) { ZipClass.UnZip(Application.StartupPath + @"\" + zip, "", maximum, FrmUpdate_SetProgress); File.Delete(Application.StartupPath + @"\" + zip); } FrmUpdate_CloseProgress();//调用关闭进度条事件 } }
//http://www.cnblogs.com/thornfield_he/archive/2009/09/22/1571914.html public UpdateBackground() { IniClass iniC = new IniClass(Application.StartupPath + @"\Update.ini"); string versionC = iniC.IniReadValue("update", "version"); HOST = "http://" + iniC.IniReadValue("update", "host") + "/EduApi/download/"; inifileUrl = HOST + "update.ini"; log("versionC=" + versionC, true); if (versionC == null || versionC.Length == 0) { return; } //////////////////////////////////////////////// //step1. check ini //////////////////////////////////////////////// string sDir = Application.StartupPath; string sPath1 = sDir + @"\updateS.ini"; string versionS = ""; try { WebClient wc = new WebClient(); log("download now..." + inifileUrl); wc.DownloadFile(new Uri(inifileUrl), sPath1);//download ini file IniClass iniS = new IniClass(sPath1); versionS = iniS.IniReadValue("update", "version"); zipfile = iniS.IniReadValue("update", "zipfile"); zipfileUrl = HOST + zipfile; zipfilePath = sDir + "\\" + zipfile; log("versionS=" + versionS + ", zipfile=" + zipfile); File.Delete(sPath1); if (versionS.CompareTo(versionC) <= 0) { log("Exit!"); return; } } catch (Exception e1) { string msg = e1.Message; log("download error..." + msg); #if DEBUG MessageBox.Show("获取升级配置文件失败:\r\n" + msg, "Warning!!!"); #endif return; } //////////////////////////////////////////////// //step2. download //////////////////////////////////////////////// { WebClient _wc = new WebClient(); log("download now..." + zipfileUrl); _wc.DownloadFile(zipfileUrl, zipfilePath);//download zip file log("download over..." + zipfilePath); } //////////////////////////////////////////////// //step3. kill & unzip //////////////////////////////////////////////// log("kill process of ruehelper.exe now..."); KillProcess(ApplicationName); { Thread.Sleep(1000); log("unzip now..."); ZipClass.UnZip(zipfilePath, "", 1, null); File.Delete(zipfilePath); } //////////////////////////////////////////////// //step4. replace //////////////////////////////////////////////// log("set update.version=" + versionS); iniC.IniWriteValue("update", "version", versionS);//更新成功后将版本写入配置文件 //////////////////////////////////////////////// //step5. run //////////////////////////////////////////////// log("restart RueHelper.exe now..."); Application.Exit();//退出升级程序 log("Process.Start(" + sDir + "\\" + ApplicationName + ") now..."); try { Process.Start(sDir + "\\" + ApplicationName);//打开主程序Main.exe } catch (Exception e) { log("Process.Start exception. " + e.Message); } }