利用webservice實現軟件自動更新(auto update)

winform程序相對web程序而言,功能更強大,編程更方便,但軟件更新卻相當麻煩,要到客戶端一臺一臺地升級,面對這個實際問題,在最近的一個小項目中,本人設計了一個通過軟件實現自動升級技術方案,彌補了這一缺陷,有較好的參考價值。

一、升級的好處。
長期以來,廣大程序員爲到底是使用Client/Server,還是使用Browser/Server結構爭論不休,在這些爭論當中,C/S結構的程序的可維護性差,佈置困難,升級不方便,維護成本高就是一個相當重要的因素,也是那些B/S的支持者們將Client/Server結構打入地獄的一個重要原因。
現在好了,我們就在最新的基於Microsoft 的 WinForm上用WebServices來實現軟件的自動升級功能。
二、升級的技術原理。
升級的原理有好幾個,首先無非是將現有版本與最新版本作比較,發現最新的則提示用戶是否升級。當然也有人用其它屬性比較的,例如:文件大小。:) 或者更新日期。
三、在.Net時代的實現。
在.Net時代,我們就有了更多的選擇,可以使用WebRequest,也可以使用WebServices。在這裏我們將用WebServices來實現軟件的自動升級。
實現原理:在WebServices中實現一個GetVer的WebMethod方法,其作用是獲取當前的最新版本。
然後將現在版本與最新版本比較,如果有新版本,則進行升級。
步驟:
1、準備一個XML文件 (Update.xml)。
view plaincopy to clipboardprint?
<?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>  
<?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> 
作用是作爲一個升級用的模板。
2、WebServices的GetVer方法。
          view plaincopy to clipboardprint?
[WebMethod(Description="取得更新版本")]  
          public string GetVer()  
          {  
                XmlDocument doc = new XmlDocument();  
                doc.Load(Server.MapPath("update.xml"));  
                XmlElement root = doc.DocumentElement;  
                return root.SelectSingleNode("version").InnerText;  
          } 
  [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方法。
           view plaincopy to clipboardprint?
[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"));  
                 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<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);  
                       //這裏是文件的實際內容,使用了Base64String編碼  
                       itemNode.SelectSingleNode("value").InnerText =   
onvert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);  
                       br.Close();  
                       fs.Close();  
                 }  
                 return doc;  
           } 
 [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"));
                  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<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);
                        //這裏是文件的實際內容,使用了Base64String編碼
                        itemNode.SelectSingleNode("value").InnerText =
Convert.ToBase64String(br.ReadBytes((int)fs.Length),0,(int)fs.Length);
                        br.Close();
                        fs.Close();
                  }
                  return doc;
            }
4、在客戶端進行的工作。
首先引用此WebServices,例如命名爲:WebSvs,
view plaincopy to clipboardprint?
string nVer = Start.GetService.GetVer();   
if(Application.ProductVersion.CompareTo(nVer)<=0)   
update();  
string nVer = Start.GetService.GetVer(); 
if(Application.ProductVersion.CompareTo(nVer)<=0)
update(); 
  在本代碼中 Start.GetService是WebSvs的一個Static 實例。首先檢查版本,將結果與當前版本進行比較,
如果爲新版本則執行UpDate方法。
          view plaincopy to clipboardprint?
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();  
          } 
  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();
            }
這裏爲了簡單起見,沒有使用異步方法,當然使用異步方法能更好的提高客戶體驗,這個需要讀者們自己去添加。
:) update的作用是將升級的XML文件下載下來,保存爲執行文件目錄下的一個Update.xml文件。任務完成,
退出程序,等待Update.Exe 來進行升級。
 
    5、Update.Exe 的內容。
 
          view plaincopy to clipboardprint?
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 = "正在重新啓動應用程序...";  
                System.Diagnostics.Process.Start("CustomerApplication.exe");  
                Close();  
                Application.Exit();  
          } 
  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 = "正在重新啓動應用程序...";
                  System.Diagnostics.Process.Start("CustomerApplication.exe");
                  Close();
                  Application.Exit();
            }
 這個代碼也很容易懂,首先就是找到主進程,如果沒有關閉,則用Process.Kill()來關閉主程序。然後則用一個
XmlDocument來Load程序生成的update.xml文件。用xml文件裏指定的路徑和文件名來生成指定

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章