1. 程式人生 > >winform應用程式自動更新版本

winform應用程式自動更新版本

我現在做的是由更新程式來更新winform程式,但是最後的目的是想winform自己本身能夠更新吧,還是沒找到好的方法,現記錄下有update.exe來更新winform.exe。

1.先建一個記錄有版本資訊的xml檔案,update.xml

其中的程式碼如下:

<root xmlns="">
  <module>
    <moduleName>BLL.dll</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/BLL.dll</appUrl>
  </module>
  <module>
    <moduleName>DAL.dll</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/DAL.dll</appUrl>
  </module>
  <module>
    <moduleName>winform.exe</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/demo.exe</appUrl>
  </module>
  <module>
    <moduleName>Model.dll</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/Model.dll</appUrl>
  </module>
  <module>
    <moduleName>RdCard.dll</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/RdCard.dll</appUrl>
  </module>
  <module>
    <moduleName>sdtapi.dll</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/sdtapi.dll</appUrl>
  </module>
  <module>
    <moduleName>WltRS.dll</moduleName>
    <version>4.0</version>
    <appUrl>http://10.77.150.105:7003/webservice/Release/Wltrs.dll</appUrl>
  </module>
</root>

update.xml是放在已經部署的webservice資料夾下的。

下面是update.exe程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Update
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, 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() == "winform")//停止所有叫"winform"的程式
                {
                    p.Kill();
                    break;
                }
            }
             string path = Application.StartupPath;
            this.VersionCheck(path, "

http://10.77.150.105:7003/webservice/myservice.asmx");//webservice地址,我是已經部署到IIS上了,我的更新的版本也在這個目錄裡面
        }
        public string GetVer()//獲得版本號
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"update.xml");
            XmlElement root = doc.DocumentElement;
            return root.SelectSingleNode("version").InnerText;
        }
    
    
        private void DownloadFile(string source, string fileName)//下載更新檔案
        {
            try
            {
                System.Net.WebClient myWebClient = new System.Net.WebClient();
                myWebClient.DownloadFile(source, fileName);
            }
            catch (Exception ex)
            {
                throw new Exception("下載失敗,原因是:" + ex.Message, ex);
            }
        }
        private void VersionCheck(string desPath, string webServiceAddress)//版本號校驗
        {
            try
            {
                #region 檢視檔案和目錄
                if (!desPath.EndsWith(@"/"))
                    desPath += @"/";

                if (!System.IO.Directory.Exists(desPath))
                {
                    System.IO.Directory.CreateDirectory(desPath);
                }

                string tempPath = desPath + @"update/";

                if (System.IO.Directory.Exists(tempPath))
                {
                    System.IO.Directory.Delete(tempPath, true);
                    System.IO.Directory.CreateDirectory(tempPath);
                }
                else
                    System.IO.Directory.CreateDirectory(tempPath);

                if (!System.IO.File.Exists(desPath + "UpdateConfig.xml"))
                {
                    System.Xml.XmlDocument updateConfig = new System.Xml.XmlDocument();
                    updateConfig.LoadXml(@"<root></root>");
                    updateConfig.Save(desPath + "UpdateConfig.xml");
                }
                #endregion

                localhost.MyService appUpdate = new localhost.MyService();
                //WebService appUpdate = new WebService();
                appUpdate.Url = webServiceAddress;
                //System.Xml.XmlDocument serverXmlDoc = ((System.Xml.XmlDocument)appUpdate.AppUpdateVertion());
                // System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
                System.Xml.XmlDocument serverXmlDoc = new XmlDocument();
                //serverXmlDoc.LoadXml();
                //System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
                System.Xml.XmlNode xml = appUpdate.AppUpdateVertion();
                serverXmlDoc.LoadXml(xml.OuterXml);
                System.Xml.XmlDocument localXmlDoc = new System.Xml.XmlDocument();
                localXmlDoc.Load(desPath + "UpdateConfig.xml");
                bool newVersionExist = false;
                bool moduleExist = false;
                System.Xml.XmlNode serverNode0 = serverXmlDoc.ChildNodes[0];
                System.Xml.XmlNode localNode0 = localXmlDoc.ChildNodes[0];
                foreach (System.Xml.XmlNode serverNode in serverNode0)
                {
                    moduleExist = false;
                    foreach (System.Xml.XmlNode localNode in localNode0)
                    {
                        //找到對應模組
                        if (localNode.ChildNodes[0].InnerText == serverNode.ChildNodes[0].InnerText)
                        {
                            moduleExist = true;
                            //版本號判斷
                            if (localNode.ChildNodes[1].InnerText.CompareTo(serverNode.ChildNodes[1].InnerText) < 0)
                            {
                                newVersionExist = true;
                                DownloadFile(serverNode.ChildNodes[2].InnerText, tempPath + serverNode.ChildNodes[0].InnerText);
                            }
                            break;
                        }
                    }
                    //沒找到對應模組
                    if (false == moduleExist)
                    {
                        DownloadFile(serverNode.ChildNodes[2].InnerText, tempPath + serverNode.ChildNodes[0].InnerText);
                        newVersionExist = true;
                    }
                }
                //寫入新UpdateConfig.xml升級完畢後替換
                if (newVersionExist)
                {
                    serverXmlDoc.Save(tempPath + "UpdateConfig.xml");
                    if (DialogResult.Yes == MessageBox.Show("有新版本,升級否", "提示", MessageBoxButtons.YesNo))
                    {
                        string[] dirs = System.IO.Directory.GetFiles(tempPath, "*.*");
                        string fileName;
                        foreach (string dir in dirs)
                        {
                            fileName = ((dir.Split(Convert.ToChar(@"/")))[dir.Split(Convert.ToChar(@"/")).Length - 1]);
                            if (System.IO.File.Exists(desPath + fileName))
                            {
                                //TODO:可以支援備份以前版本
                                //System.Diagnostics.Process.Start(Application.StartupPath + @"\demo.exe");
                                //System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();

                                System.IO.File.Delete(desPath + fileName);
                            }
                            //TODO:如果系統正在執行,您得停止系統,至於如何停止,也許可以使用System.Diagnostics.Process
                          
                           System.IO.File.Move(dir, desPath + fileName);
                        }
                        MessageBox.Show("升級完畢");
                    }
                    else
                    {
                        //TODO:可以支援重新提示升級
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("升級失敗,原因是:" + ex.Message, ex);
            }
            System.Diagnostics.Process.Start("demo.exe");
            Close();
            Application.Exit();
        }
   }
}

update.exe還需要新增引用webservice,webservice上面的程式碼:

  [WebMethod(Description = "讀伺服器上關於程式各個版本資訊的配置檔案UpdateConfig.xml")]
    public System.Xml.XmlDocument AppUpdateVertion()
    {
        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
        xml.Load(CurrentPath("/webs1") + @"/UpdateConfig.xml");
        return xml;
    }
    private string CurrentPath(string virtualPath)
    {
        return HttpContext.Current.Server.MapPath(virtualPath);
    }

主要原理是,停止所有的winform.exe,update會建一個資料夾叫update(在程式的Update\bin\Debug目錄下),先判斷版本號,如果版本號比原來的大,則更新,若不是,則執行原版本的應用程式,若有新的版本,就會,update資料夾內是存放下載的新的版本的winform.exe和其他檔案,之後,刪除update.exe資料夾的debug資料夾下的相對的資料夾,然後將update資料夾下面的檔案移出到debug資料夾下面,最後啟動winform.exe。

不懂得可以留言!!!