Winform 程序自动更新

一、WebService

1.1 编写自动更新WebService

 读服务器上关于程序各个版本信息的配置文件UpdateConfig.xml
    [WebMethod]
    public System.Xml.XmlDocument AppUpdateVertion()
    {
        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
        xml.Load(CurrentPath("/IEMS_OP_AppUpdate") + @"/UpdateConfig.xml");
        return xml;
    }
    private string CurrentPath(string virtualPath)
    {
        return HttpContext.Current.Server.MapPath(virtualPath);
    }  

1.2 UpdateConfig.xml配置文件

配置文件说明:module--模块信息,
                            version--版本信息,
                            appUrl--更新文件在服务器存放的路径
<root xmlns="">
  <module>
    <moduleName>Biz.dll</moduleName>
    <version>1.0.0.1</version>
    <appUrl>http://localhost/IEMS_OP_AppUpdate/1.0.0.1/Biz.dll</appUrl>
  </module>
  <module>
    <moduleName>CodeList.dll</moduleName>
    <version>1.0.0.1</version>
    <appUrl>http://localhost/IEMS_OP_AppUpdate/1.0.0.1/CodeList.dll</appUrl>
  </module>
</root>

1.3 生成WebService代理类

使用.Net命令行wsdl,生成WebService代理类

二、自动更新程序

2.1 文件下载

        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);
            }
        }


2.2 校验版本信息
        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 + @"tempDesPathCache/";

                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

                WebService appUpdate = new WebService();
                appUpdate.Url = webServiceAddress;
                System.Xml.XmlDocument serverXmlDoc = appUpdate.AppUpdateVertion();
                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.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);
            }
        }

2.3 运新更新
           
string path = Application.StartupPath;
            this.VersionCheck(path, "http://localhost:4011/AppUpdate/WebService.asmx");

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值