上篇文章主要介紹了webservice的部署以及主要的代碼,下面給大家貼上主程序的代碼,並簡單的講解。主程序判斷是否有更新時通過主程序目錄下有一個update.ini文件內的version是否有變化,主要思路為判斷客戶端的version是否==服務器端webservice提取到的version的值,update.ini文件內容如下:
[update] version=20132103 //通過這個客戶端的version於服務器端的version相比較,如果不等於則為更新 program=BJCreation.CallAppWindow.Windows.exe //主程序的目錄,也就是誰要啟動這個update.exe [webservice] url=http://localhost:1546/UpdateWeb/sxsUpdateWebService.asmx //這個就是webservice的地址 method=GetUpdateInfos //webservice的方法名
//關閉進度條的委托 public delegate void CloseProgressDelegate(); public delegate void OnGoToShowLogDegate(); public delegate void OnGoTOShowRtbLogDelate(object message); //聲明關閉進度條事件 public event CloseProgressDelegate CloseProgress; string serviceVersion; WebClient wc = null; IniClass ini; //ini文件的操作 string url;//獲取下載地址 string[] zips;//獲取升級壓縮包 string zip = "";//當前正在下載的壓縮包 string uploadLog = string.Empty; //更新日志 int zipsIndex = 0;//當前正在下載的zips下標 long preBytes = 0;//上一次下載流量 long currBytes = 0;//當前下載流量 private static int time = 3;//3秒自動退出,沒有更新的話自動退出 string filePath = string.Empty;
public FormUpdate() { InitializeComponent(); this.btnDownload.Enabled = false; ini = new IniClass(Application.StartupPath + @"\Update.ini"); Thread thread = new Thread(new ThreadStart(ThreadMethod)); thread.Start(); } /// <summary> /// 如果將更新程序放到根目錄下面的子目錄內,如Realeas/Update/update.exe,則啟動時需要傳遞參數調用這個方法 /// 主程序的調用方式如下:process.start("update/update.exe","\""+Application.StartupPath+"\"");因為如果當前目錄含有空格類似c:\programes files\程序1 這樣的目錄,用start方式啟動不成功,必須要將start的參數2改為短文件名 或者直接加""的方式 /// 構造窗體 /// </summary> /// <param name="path"></param> public FormUpdate( string path ) { this.filePath = path; InitializeComponent(); this.btnDownload.Enabled = false; ini = new IniClass( Application.StartupPath + @"\Update.ini" ); Thread thread = new Thread( new ThreadStart( ThreadMethod ) ); thread.Start(); }
void ThreadMethod() { try { string webServiceUrl = ini.IniReadValue("webservice", "url");//wsdl地址 string method = ini.IniReadValue("webservice", "method");// string[] strArray = (string[])WebServiceHelper.InvokeWebService(webServiceUrl, method, null);//調用webservice並返回結果 uploadLog = strArray[3]; zips = strArray[2].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); url = strArray[1]; string clentVersion = ini.IniReadValue("update", "version"); serviceVersion = strArray[0];//服務端版本 if (!string.Equals(clentVersion, serviceVersion)) {//如果不等則直接顯示更新日志及更新列表 OnGoToShowLogDegate logHandler = new OnGoToShowLogDegate(ShowLog); this.Invoke(logHandler); } else { OnGoToShowLogDegate logHandler = new OnGoToShowLogDegate(ShowRtbLogView); this.Invoke(logHandler); } } catch (Exception ex) { OnGoTOShowRtbLogDelate logHandler = new OnGoTOShowRtbLogDelate(ShowRtbLog); this.Invoke(logHandler, new object[] { ex.Message }); } }
void ShowLog() { this.groupBox1.Text = serviceVersion + "更新日志"; this.rtbUpdateLog.Text = string.IsNullOrEmpty(uploadLog) ? "暫時無更新" : uploadLog; if (zips.Length > 0) { foreach (string item in zips) { string size = ""; try { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(new Uri(url + item)); HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse(); size = GetFileSize(myRes.ContentLength); myRes.Close(); } catch (Exception) { //如果有異常則不處理 } ListViewItem lsv = new ListViewItem(); lsv.Text = item; lsv.SubItems.Add(size); this.lsvFiles.Items.Add(lsv); } this.btnDownload.Enabled = true; } } private string GetFileSize(long size) { string msg = "0B"; if (size == 0) { } else if (size > 0 && size < 1024) { msg = size + "B"; } else if (size > 1024 && size < 1024 * 1024) { msg = (size / 1024) + "KB"; } else if (size > 1024 * 1024 && size < 1024 * 1024 * 1024) { msg = size / (1024 * 1024) + "MB"; } return msg; }
/// <summary> /// 下載更新 /// </summary> private void DownLoad() { try { CloseProgress += new CloseProgressDelegate(FrmUpdate_CloseProgress); if (zips.Length > 0) { 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 { FrmUpdate_CloseProgress();//調用關閉進度條事件 } } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 下載完成後觸發 /// </summary> void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { try { if (zipsIndex < zips.Length) { //繼續下載下一個壓縮包 wc = new WebClient(); wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted); string url2 = url + zips[zipsIndex]; wc.DownloadFileAsync(new Uri(url2), zips[zipsIndex]); } else { //解壓 string _path = string.IsNullOrEmpty(filePath) ? Application.StartupPath : filePath; int maximum = ZipClass.GetMaximum(zips, _path); foreach (string zip in zips) { string fileName = _path + @"\" + zip; string extension = Path.GetExtension(fileName); if (extension != ".zip") { //如果不是zip的文件則不允許解壓。 continue; } ZipClass.UnZip(_path + @"\" + zip, "", maximum, FrmUpdate_SetProgress); File.Delete(_path + @"\" + zip); } FrmUpdate_CloseProgress();//調用關閉進度條事件 } //此行放置在此處 zipsIndex++; } catch (Exception ex) { Log.LogToFile(ex.Message); } } /// <summary> /// 下載時觸發 /// </summary> void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { if (this.InvokeRequired) { this.Invoke(new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged), new object[] { sender, e }); } else { try { lblMessage.Text = "正在下載自解壓包 " + zips[zipsIndex] + "(" + (zipsIndex + 1).ToString() + "/" + zips.Length + ")"; progressBar1.Maximum = 100; progressBar1.Value = e.ProgressPercentage; currBytes = e.BytesReceived;//當前下載流量 } catch (Exception) { //如果出錯,直接運行。 } } } /// <summary> /// 解壓時進度條事件 /// </summary> /// <param name="maximum">進度條最大值</param> private void FrmUpdate_SetProgress(int maximum, string msg) { if (this.InvokeRequired) { this.Invoke(new ZipClass.SetProgressDelegate(FrmUpdate_SetProgress), new object[] { maximum, msg }); } else { timer1.Enabled = false; //this.Text = "升級"; if (zipsIndex == zips.Length) { //剛壓縮完 progressBar1.Value = 0; zipsIndex++; } lblMessage.Text = "正在解壓" + msg + "(" + (progressBar1.Value + 1).ToString() + "/" + maximum + ")"; progressBar1.Maximum = maximum; progressBar1.Value++; } }
----
Process _process = new Process(); _process.StartInfo.FileName = "Update/BJCreation.AutoUpdate.Windows.exe"; _process.StartInfo.Arguments="\""+ Application.StartupPath+"\""; _process.StartInfo.WorkingDirectory =Application.StartupPath; _process.Start();