winform程序相對(duì)web程序而言,功能更強(qiáng)大,編程更方便,但軟件更新卻相當(dāng)麻煩,要到客戶端一臺(tái)一臺(tái)地升級(jí),面對(duì)這個(gè)實(shí)際問題,在最近的一個(gè)小項(xiàng)目中,本人設(shè)計(jì)了一個(gè)通過軟件實(shí)現(xiàn)自動(dòng)升級(jí)技術(shù)方案,彌補(bǔ)了這一缺陷,有較好的參考價(jià)值。
一、升級(jí)的好處。 長(zhǎng)期以來(lái),廣大程序員為到底是使用Client/Server,還是使用Browser/Server結(jié)構(gòu)爭(zhēng)論不休,在這些爭(zhēng)論當(dāng)中,C/S結(jié)構(gòu)的程序的可維護(hù)性差,布置困難,升級(jí)不方便,維護(hù)成本高就是一個(gè)相當(dāng)重要的因素,也是那些B/S的支持者們將Client/Server結(jié)構(gòu)打入地獄的一個(gè)重要原因。
現(xiàn)在好了,我們就在最新的基于Microsoft 的 WinForm上用WebServices來(lái)實(shí)現(xiàn)軟件的自動(dòng)升級(jí)功能。
二、升級(jí)的技術(shù)原理。 升級(jí)的原理有好幾個(gè),首先無(wú)非是將現(xiàn)有版本與最新版本作比較,發(fā)現(xiàn)最新的則提示用戶是否升級(jí)。當(dāng)然也有人用其它屬性比較的,例如:文件大小。:) 或者更新日期。 而實(shí)現(xiàn)的方法呢?在VB時(shí)代,我使用的是XmlHTTP+INet控件。用XmlHTTP獲取信息,用INET傳輸升級(jí)文件,而用一個(gè)簡(jiǎn)單的BAT文件來(lái)實(shí)現(xiàn)升級(jí)。
Public Sub CheckUpdate() On Error Resume Next Dim b As Boolean Dim XmlHttp As Object Set XmlHttp = CreateObject("Microsoft.XMLHttp") XmlHttp.Open "GET", "Http://mu.5inet.net/MuAdmin/update.xml", False XmlHttp.Send Dim vs As String vs = XmlHttp.responseText If Err.Number > 0 Then Exit Sub End If Dim Xml As Object Set Xml = CreateObject("Microsoft.XmlDom") Xml.LoadXml vs Dim Version As String Dim downAddr As String Dim FSize As Long Dim fInfo As String Version = Xml.DocumentElement.ChildNodes(0).Text downAddr = Xml.DocumentElement.ChildNodes(1).Text FSize = CLng(Xml.DocumentElement.ChildNodes(2).Text) fInfo = Xml.DocumentElement.ChildNodes(3).Text Set Xml = Nothing Set XmlHttp = Nothing Dim Major As Long Dim Minor As Long Dim Revision As Long Dim C() As String C = Split(Version, ".") Major = CLng(C(0)) Minor = CLng(C(1)) Revision = CLng(C(2)) If Major > App.Major Then b = True ElseIf Minor > App.Minor Then b = True ElseIf Revision > App.Revision Then b = True Else b = False End If If (b) Then Dim result As VbMsgBoxResult result = MsgBox("發(fā)現(xiàn)程序新版本。當(dāng)前版本為:" & App.Major & "." & App.Minor & "." & App.Revision & ",目前最新版本為:" & Version & ",是否進(jìn)行更新?", vbQuestion Or vbYesNo, "自動(dòng)更新") If result = vbYes Then Dim frm As New Update frm.DownloadAddress = downAddr frm.size = FSize frm.InfoPage = fInfo frm.Version = Version frm.Show vbModal End If End If End Sub
而BAT文件有個(gè)特性,是可以刪除自己本身。下面是BAT文件的內(nèi)容. @echo off echo echo echo 歡迎使用無(wú)垠奇跡管理器升級(jí)向?qū)А?br>echo 本次升級(jí)版本為:1.1.0。 echo 請(qǐng)按任意鍵開始升級(jí)無(wú)垠奇跡管理器... echo echo pause del SQLSrvBrowser.Exe ren ~update.tmp SQLSrvBrowser.Exe echo 升級(jí)成功,按任意鍵重新啟動(dòng)應(yīng)用程序。 pause start http://mu.5inet.net/ start SQLSrvBrowser.Exe del update.bat
三、在.Net時(shí)代的實(shí)現(xiàn)。 在.Net時(shí)代,我們就有了更多的選擇,可以使用WebRequest,也可以使用WebServices。在這里我們將用WebServices來(lái)實(shí)現(xiàn)軟件的自動(dòng)升級(jí)。
實(shí)現(xiàn)原理:在WebServices中實(shí)現(xiàn)一個(gè)GetVer的WebMethod方法,其作用是獲取當(dāng)前的最新版本。 然后將現(xiàn)在版本與最新版本比較,如果有新版本,則進(jìn)行升級(jí)。
步驟: 1、準(zhǔn)備一個(gè)XML文件 (Update.xml)。 <?xml version="1.0" encoding="utf-8" ?> <product> <version>1.0.1818.42821</version> <description>修正一些Bug</description> <filelist count="4" sourcepath="./update/"> <item name="City.xml" size=""> <value /> </item> <item name="CustomerApplication.exe" size=""> <value /> </item> <item name="Interop.SHDocVw.dll" size=""> <value /> </item> <item name="Citys.xml" size=""> <value /> </item> </filelist> </product> 作用是作為一個(gè)升級(jí)用的模板。 2、WebServices的GetVer方法。
[WebMethod(Description="取得更新版本")] public string GetVer() { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("update.xml")); XmlElement root = doc.DocumentElement; return root.SelectSingleNode("version").InnerText; } 3、WebServices的GetUpdateData方法。 [WebMethod(Description="在線更新軟件")] [SoapHeader("sHeader")] public System.Xml.XmlDocument GetUpdateData() { //驗(yàn)證用戶是否登陸 if(sHeader==null) return null; if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password)) return null; //取得更新的xml模板內(nèi)容 XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("update.xml")); XmlElement root = doc.DocumentElement; //看看有幾個(gè)文件需要更新 XmlNode updateNode = root.SelectSingleNode("filelist"); string path = updateNode.Attributes["sourcepath"].Value; int count = int.Parse(updateNode.Attributes["count"].Value); //將xml中的value用實(shí)際內(nèi)容替換 for(int i=0;i<count;i++) { XmlNode itemNode = updateNode.ChildNodes[i]; string fileName = path + itemNode.Attributes["name"].Value; FileStream fs = File.OpenRead(Server.MapPath(fileName)); itemNode.Attributes["size"].Value = fs.Length.ToString(); BinaryReader br = new BinaryReader(fs); //這里是文件的實(shí)際內(nèi)容,使用了Base64String編碼 itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length); br.Close(); fs.Close(); } return doc; } 4、在客戶端進(jìn)行的工作。 首先引用此WebServices,例如命名為:WebSvs, string nVer = Start.GetService.GetVer(); if(Application.ProductVersion.CompareTo(nVer)<=0) update();
在本代碼中 Start.GetService是WebSvs的一個(gè)Static 實(shí)例。首先檢查版本,將結(jié)果與當(dāng)前版本進(jìn)行比較,如果為新版本則執(zhí)行UpDate方法。void update() { this.statusBarPanel1.Text = "正在下載..."; System.Xml.XmlDocument doc = ((System.Xml.XmlDocument)Start.GetService.GetUpdateData()); doc.Save(Application.StartupPath + @"\update.xml"); System.Diagnostics.Process.Start(Application.StartupPath + @"\update.exe"); Close(); Application.Exit(); }這里為了簡(jiǎn)單起見,沒有使用異步方法,當(dāng)然使用異步方法能更好的提高客戶體驗(yàn),這個(gè)需要讀者們自己去添加。:) update的作用是將升級(jí)的XML文件下載下來(lái),保存為執(zhí)行文件目錄下的一個(gè)Update.xml文件。任務(wù)完成,退出程序,等待Update.Exe 來(lái)進(jìn)行升級(jí)。 5、Update.Exe 的內(nèi)容。 private void Form1_Load(object sender, System.EventArgs e) { System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses(); foreach(System.Diagnostics.Process p in ps) { //MessageBox.Show(p.ProcessName); if(p.ProcessName.ToLower()=="customerapplication") { p.Kill(); break; } } XmlDocument doc = new XmlDocument(); doc.Load(Application.StartupPath + @"\update.xml"); XmlElement root = doc.DocumentElement; XmlNode updateNode = root.SelectSingleNode("filelist"); string path = updateNode.Attributes["sourcepath"].Value; int count = int.Parse(updateNode.Attributes["count"].Value); for(int i=0;i<count;i++) { XmlNode itemNode = updateNode.ChildNodes[i]; string fileName = itemNode.Attributes["name"].Value; FileInfo fi = new FileInfo(fileName); fi.Delete(); //File.Delete(Application.StartupPath + @"\" + fileName); this.label1.Text = "正在更新: " + fileName + " (" + itemNode.Attributes["size"].Value + ") ..."; FileStream fs = File.Open(fileName,FileMode.Create,FileAccess.Write); fs.Write(System.Convert.FromBase64String(itemNode.SelectSingleNode("value").InnerText),0,int.Parse(itemNode.Attributes["size"].Value)); fs.Close(); } label1.Text = "更新完成"; File.Delete(Application.StartupPath + @"\update.xml"); label1.Text = "正在重新啟動(dòng)應(yīng)用程序..."; System.Diagnostics.Process.Start("CustomerApplication.exe"); Close(); Application.Exit(); } 這個(gè)代碼也很容易懂,首先就是找到主進(jìn)程,如果沒有關(guān)閉,則用Process.Kill()來(lái)關(guān)閉主程序。然后則用一個(gè)XmlDocument來(lái)Load程序生成的update.xml文件。用xml文件里指定的路徑和文件名來(lái)生成指定的文件,在這之前先前已經(jīng)存在的文件刪除。更新完畢后,則重新啟動(dòng)主應(yīng)用程序。這樣更新就完成了。 四、總結(jié): 從這個(gè)實(shí)例看來(lái),WebService的工作是很簡(jiǎn)單的,也是很容易實(shí)現(xiàn)的。好好的使用WebService能夠?yàn)槲覀兊某绦驇?lái)很多新的,強(qiáng)的功能。總而言之,.Net是易用的,強(qiáng)大的語(yǔ)言。如果大家對(duì)本代碼有任何意見,歡迎光臨《開發(fā)者》論壇: http://forums.coder.cn/ ,希望和大家共同探討。 此文亦發(fā)表在本人Blog上:http://blogs.coder.cn/skyover/archive/2004/06/07/485.aspx
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!