Exemple #1
0
        /// <summary>
        /// 检查新版本
        /// </summary>
        /// <exception cref="System.Net.WebException">无法找到指定资源</exception>
        /// <exception cref="System.NotSupportException">升级地址配置错误</exception>
        /// <exception cref="System.Xml.XmlException">下载的升级文件有错误</exception>
        /// <exception cref="System.ArgumentException">下载的升级文件有错误</exception>
        /// <exception cref="System.Excpetion">未知错误</exception>
        /// <returns></returns>
        public void Update()
        {
            if (!updateconfig.Enabled)
            {
                return;
            }

            /*
             * 请求Web服务器,得到当前最新版本的文件列表,格式同本地的FileList.xml。
             * 与本地的FileList.xml比较,找到不同版本的文件
             * 生成一个更新文件列表,开始DownloadProgress
             * <UpdateFile>
             *  <File path="" url="" lastver="" size=""></File>
             * </UpdateFile>
             * path为相对于应用程序根目录的相对目录位置,包括文件名
             */
            WebClient client = new WebClient();
            string    strXml = client.DownloadString(updateconfig.ServerUrl);

            Dictionary <string, RemoteFile> listRemotFile = ParseRemoteXml(strXml);

            List <DownloadFileInfo> downloadList = new List <DownloadFileInfo>();

            //某些文件不再需要了,删除
            List <LocalFile> preDeleteFile = new List <LocalFile>();

            foreach (LocalFile file in updateconfig.UpdateFileList)
            {
                if (listRemotFile.ContainsKey(file.Path))
                {
                    RemoteFile rf = listRemotFile[file.Path];
                    //file.LastVer本地版本
                    //rf.LastVer 服务器版本
                    //分割   一个一个小版本比较
                    //if(Convert.ToDecimal( file.LastVer) > Convert.ToDecimal(rf.LastVer))
                    //{
                    //    MessageBox.Show("最新");
                    //}

                    if (rf.LastVer != file.LastVer)
                    {
                        downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size, rf.ReMark));
                        file.LastVer = rf.LastVer;
                        file.Size    = rf.Size;

                        if (rf.NeedRestart)
                        {
                            bNeedRestart = true;
                        }
                    }

                    listRemotFile.Remove(file.Path);
                }
                else
                {
                    preDeleteFile.Add(file);
                }
            }

            foreach (RemoteFile file in listRemotFile.Values)
            {
                downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size, file.ReMark));
                updateconfig.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size, file.ReMark));

                if (file.NeedRestart)
                {
                    bNeedRestart = true;
                }
            }

            if (downloadList.Count > 0)
            {
                DownloadConfirm dc = new DownloadConfirm(downloadList);

                if (this.OnShow != null)
                {
                    this.OnShow();
                }

                if (DialogResult.OK == dc.ShowDialog())
                {
                    foreach (LocalFile file in preDeleteFile)
                    {
                        string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.Path);
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }

                        updateconfig.UpdateFileList.Remove(file);
                    }

                    StartDownload(downloadList);
                }
            }
        }
        public void Update()
        {
            if (!updateconfig.Enabled)
            {
                return;
            }

            WebClient client = new WebClient();
            string    strXml = client.DownloadString(updateconfig.ServerUrl);

            Dictionary <string, RemoteFile> listRemotFile = ParseRemoteXml(strXml);

            List <DownloadFileInfo> downloadList = new List <DownloadFileInfo>();

            List <LocalFile> preDeleteFile = new List <LocalFile>();

            foreach (LocalFile file in updateconfig.UpdateFileList)
            {
                if (listRemotFile.ContainsKey(file.Path))
                {
                    RemoteFile rf = listRemotFile[file.Path];


                    if (rf.LastVer != file.LastVer)
                    {
                        downloadList.Add(new DownloadFileInfo(rf.Url, file.Path, rf.LastVer, rf.Size, rf.ReMark));
                        file.LastVer = rf.LastVer;
                        file.Size    = rf.Size;

                        if (rf.NeedRestart)
                        {
                            bNeedRestart = true;
                        }
                    }

                    listRemotFile.Remove(file.Path);
                }
                else
                {
                    preDeleteFile.Add(file);
                }
            }

            foreach (RemoteFile file in listRemotFile.Values)
            {
                downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size, file.ReMark));
                updateconfig.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size, file.ReMark));

                if (file.NeedRestart)
                {
                    bNeedRestart = true;
                }
            }

            if (downloadList.Count > 0)
            {
                DownloadConfirm dc = new DownloadConfirm(downloadList);

                if (this.OnShow != null)
                {
                    this.OnShow();
                }

                if (DialogResult.OK == dc.ShowDialog())
                {
                    foreach (LocalFile file in preDeleteFile)
                    {
                        string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.Path);
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }

                        updateconfig.UpdateFileList.Remove(file);
                    }

                    StartDownload(downloadList);
                }
            }
        }