Exemple #1
0
        /// <summary>
        /// 检查版本更新
        /// </summary>
        /// <param name="xmlPath">本地版本信息xml所在目录</param>
        /// <param name="webServiceAddress">服务器版本信息xml的所在目录URL</param>
        /// <returns>是否有更新</returns>
        public bool VersionCheck(string xmlPath, string webServiceAddress) //版本号校验
        {
            var newVersionExist = false;

            if (!xmlPath.EndsWith(@"/"))
            {
                xmlPath += @"/";
            }
            if (!File.Exists(xmlPath + "UpdateConfig.xml"))
            {
                var updateConfig = new XmlDocument();
                updateConfig.LoadXml(@"<root></root>");
                updateConfig.Save(xmlPath + "UpdateConfig.xml");
            }

            try
            {
                var         appUpdate    = new MyService(webServiceAddress);
                XmlDocument serverXmlDoc = appUpdate.AppUpdateVersion();
                var         localXmlDoc  = new XmlDocument();
                localXmlDoc.Load(xmlPath + @"UpdateConfig.xml");
                if (serverXmlDoc.DocumentElement != null && localXmlDoc.DocumentElement != null)
                {
                    XmlNodeList serverNodes = serverXmlDoc.DocumentElement.GetElementsByTagName("File");
                    XmlNodeList localNodes  = localXmlDoc.DocumentElement.GetElementsByTagName("File");
                    foreach (XmlNode serverNode in serverNodes)
                    {
                        var moduleExist = false;
                        foreach (XmlNode localNode in localNodes)
                        {
                            //找到对应模块
                            if (((XmlElement)localNode).GetElementsByTagName("FileName")[0].InnerText
                                != ((XmlElement)serverNode).GetElementsByTagName("FileName")[0].InnerText)
                            {
                                continue;
                            }

                            moduleExist = true;
                            bool isNew;
                            try
                            {
                                isNew = new Version(((XmlElement)localNode).GetElementsByTagName("Version")[0].InnerText).CompareTo(
                                    new Version(((XmlElement)serverNode).GetElementsByTagName("Version")[0].InnerText)) < 0;
                            }
                            catch
                            {
                                isNew = string.Compare(((XmlElement)localNode).GetElementsByTagName("Version")[0].InnerText,
                                                       ((XmlElement)serverNode).GetElementsByTagName("Version")[0].InnerText, StringComparison.Ordinal) < 0;
                            }

                            //版本号判断
                            if (isNew)
                            {
                                newVersionExist = true;
                                DataRow dr = this._neededToDownload.NewRow();
                                dr[0] = ((XmlElement)serverNode).GetElementsByTagName("FileName")[0].InnerText;
                                dr[1] = ((XmlElement)serverNode).GetElementsByTagName("SourceDir")[0].InnerText;
                                dr[2] = ((XmlElement)serverNode).GetElementsByTagName("DestDir")[0].InnerText;
                                this._neededToDownload.Rows.Add(dr);
                            }

                            break;
                        }

                        //没找到对应模块
                        if (moduleExist)
                        {
                            continue;
                        }

                        {
                            newVersionExist = true;
                            DataRow dr = this._neededToDownload.NewRow();
                            dr[0] = ((XmlElement)serverNode).GetElementsByTagName("FileName")[0].InnerText;
                            dr[1] = ((XmlElement)serverNode).GetElementsByTagName("SourceDir")[0].InnerText;
                            dr[2] = ((XmlElement)serverNode).GetElementsByTagName("DestDir")[0].InnerText;
                            this._neededToDownload.Rows.Add(dr);
                        }
                    }
                }

                this.xml = (XmlDocument)serverXmlDoc.Clone();
            }
            catch
            {
                return(false);
            }

            return(newVersionExist);
        }