【原创】自动更新程序2--更新程序的主窗体(技术:spring.net+三层架构+webservice+IrisSkin2换肤)...
??? 上篇文章主要介紹了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的方法名??? 1、變量定義
//關閉進度條的委托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;2、構造函數
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();}//通過多線程去處理webservice的讀取,防止界面卡死。webservice調用使用了代理的方式,不用添加web引用WebServiceHelper是一個類,實現了webservice的調用。只需要將webservice地址和方法名傳遞過去即可實現。
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++;}}網上客戶端升級源碼下載 ----該程序是由網上的程序修改而來,大家有興趣可以看看
UpdateWeb的源碼下載(需要的可以研究一下,Bin下面有打包好的real)
更新主程序下載(此程序只包含有windows程序,如果報錯自行引用Bin目錄下面的dll即可。)
?
?
如果需要對Update.exe程序進行調用則直接書寫
Process _process = new Process();_process.StartInfo.FileName = "Update/BJCreation.AutoUpdate.Windows.exe";_process.StartInfo.Arguments="\""+ Application.StartupPath+"\"";_process.StartInfo.WorkingDirectory =Application.StartupPath;_process.Start();如果將Update.exe放在根目錄下,則直接
Process.Start("Update.exe");即可
轉載于:https://www.cnblogs.com/smthts/archive/2013/05/15/3080971.html
總結
以上是生活随笔為你收集整理的【原创】自动更新程序2--更新程序的主窗体(技术:spring.net+三层架构+webservice+IrisSkin2换肤)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据库异地备份Shell脚本
- 下一篇: express基础一:开始