利用Web Services实现软件自动升级
摘 要:軟件維護升級工作是軟件生命周期最重要的環節。為了解決以往C/S(Client/Server)模式下的客戶端軟件升級效率低的問題,設計了C/S應用系統自動升級處理程序。該程序利用Web Services技術、C#和XML語言,通過網絡來完成C/ S應用系統的自動升級。與原有手工升級、FTP 文件服務器升級和第三方控件升級相比,升級效率更高。該方案具有較好的參考價值。
關鍵詞:C#;Web Services;XML;軟件自動升級
中圖法分類號: 文獻標志碼:
1 引言
隨著計算機網絡應用技術的不斷發展,在開發MIS系統時,大多采用基于C/S(客戶機/服務器)模式或B/S(瀏覽器/服務器)模式。現在B/S模式以其真正意義上的瘦客戶機/胖服務器模式優勢占據了主導地位。但是由于客戶機/服務器模式具有的數據流量小、響應時間短、安全性高等特點,在解決幾十個到幾百個用戶的局域網中,仍然是一個不錯的選擇[1-3]。在C/S模式下,應用程序的每次升級都需要在每個客戶端重新安裝應用程序,這是一項十分繁瑣的事情。面對這個實際問題,這里設計了一個通過軟件實現自動升級技術方案,彌補了這一缺陷,有較好的參考價值。
2 設計思路
判斷一個文件是否要更新,可以通過判斷文件的大小、修改日期和文件的版本號來實現[3-5]。發現最新的則提示用戶是否升級。
在Web Services中實現一個GetVer的WebMethod方法,其作用是獲取當前的最新版本。然后將現在版本與最新版本比較,如果有新版本,則進行升級。
3 自動升級的技術實現
(1)編寫升級模板文件Update.xml
準備一個XML文件 (Update.xml) ,作為一個升級用的模板。
……
<description>升級記錄</description>
<filelist count="4" sourcepath="./update/">
……
<item name="CustomerApplication.exe" size="">
<value />
</item>
<item name="Interop.SHDocVw.dll" size="">
<value />
</item>
……
SHDocVw.DLL 是Internet Explorer的一個組件,該組件負責控制對從Web 站點返回的URL和信息的處理。
(2)編寫Web Services的GetVer方法
GetVer方法用于取得軟件的更新版本。
[WebMethod(Description="取得更新版本")]
Public string GetVer()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));
XmlElement root = doc.DocumentElement;
return root.SelectSingleNode("version").Inertest;
}
(3)編寫Web Services的GetUpdateData方法
GetUpdateData方法用于在線更新軟件。
[WebMethod(Description="在線更新軟件")]
[SoapHeader("sHeader")]
public System.Xml.XmlDocument GetUpdateData()
{
//驗證用戶是否登陸
if(sHeader==null)
return null;
if(!DataProvider.GetInstance.CheckLogin(sHeader.Username,sHeader.Password))
return null;
//取得更新的xml模板內容
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("update.xml"));c XmlElement root = doc.DocumentElement;
//看看有幾個文件需要更新
XmlNode updateNode = root.SelectSingleNode("filelist");
string path = updateNode.Attributes["sourcepath"].Value;
int count = int.Parse(updateNode.Attributes["count"].Value);
//將xml中的value用實際內容替換
for(int i=0;i= 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);
//這里是文件的實際內容,使用了Base64String編碼
itemNode.SelectSingleNode("value").InnerText = Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
br.Close();
fs.Close();
}
return doc;
}
(4)編寫客戶端的UpDate方法
首先引用此Web Services,例如命名為:WebSvs,
string nVer = Start.GetService.GetVer();
if(Application.ProductVersion.CompareTo(nVer)<=0)
update();
在本代碼中Start.GetService是WebSvs的一個靜態實例。功能是:首先檢查版本,將結果與當前版本進行比較,如果為新版本則執行UpDate方法。
update的作用是將升級的XML文件下載下來,保存為執行文件目錄下的一個Update.xml文件。任務完成,退出程序,等待Update.Exe 來進行升級。
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();
}
(5)編寫客戶端的Update.Exe
Update.exe的功能主要是:首先就是找到主進程;如果沒有關閉,則用Process.Kill()來關閉主程序。然后則用一個XmlDocument來Load程序生成的update.xml文件。用xml文件里指定的路徑和文件名來生成指定的文件,在這之前先前已經存在的文件刪除。更新完畢后,則重新啟動主應用程序。這樣更新就完成了。
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= 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 = "正在重新啟動應用程序...";
System.Diagnostics.Process.Start("CustomerApplication.exe");
Close();
Application.Exit();
}
這里為了簡單起見,沒有使用異步方法,當然使用異步方法能更好的避免并發調用產生的沖突,這個需要讀者自己去添加。
4 結束語
借助Web Services實現軟件的自動升級,不僅設計簡單,實現起來也很容易,取得了良好的效應,大大減輕了維護的工作量。本方案具有較好的參考價值。
參考文獻
[1] 楊繼家,張麗靜,張曉蕾.面向C/S模式下的客戶端軟件自動升級的實現[J].微計算機應用,2005(5),290-293
YANG Ji-jia,ZHANG Li-jing,ZHANG Xiao-lei.An realization of Automatically updating orienting to C/S Application System[J].Microcomputer Applications,2005(5),290-293
[2] 何航校,蔣兆遠.一種改進的通用客戶端自動升級模型及實現[J].蘭州交通大學學報(自然科學版),2005(8),110-113
HE Hang-xiao,JIANG Zhao-yuan.An Improved Universal Auto Upgrade Model of Client and its Realization.Journal of Lanzhou Jiaotong University (Natural Sciences),2005(8),110-113
[3] 烏云高娃.動態升級在MIS 系統中的實現與應用[J].計算機工程與設計,2005(10),2854-2856
WUYUN Gao-wa.Implementation and application of dynamic upgrade technique in MIS[J].Computer Engineering and Design,2005(10),2854-2856
[4] 葉利華,陶宏才,梁田.基于COM的軟件在線升級技術[J].成都信息工程學院學報,2005(2),73-75
YE Li-hua,TAO Hong-cai,LIANG Tian.Software live updating technology based on COM[J].Journal of Chengdu University of Information Technology,2005(2),73-75
[5] 余穎,董旭源,高宏.C/S模式管理信息系統實現自動升級和維護的方法[J].佳木斯大學學報(自然科學版),2005(4),200-202
Implementation of software auto-update by Web Services
Abstract: The software maintaining and updating is an important section in the software life cycle. This paper makes use of the technology of Web Services、C# and XML Language, to resolve the poor efficiency of client’s updating in old C/S application system. The automatically updating program can detect the new version, download the updating file, automatically backup and rollback. It makes the software updating by the client automatically, and it is practical.
Keywords: C#;Web Services;XML;software auto-update
轉載于:https://www.cnblogs.com/China-Dragon/archive/2009/05/14/1456981.html
總結
以上是生活随笔為你收集整理的利用Web Services实现软件自动升级的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 元气骑士如何获得机器人成就皮肤_元气骑士
- 下一篇: python定义符号常量_python从