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 (!config.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(config.ServerUrl);

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

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

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

            //先判断本地和服务器的文件,对比本地更新列表
            foreach (LocalFile file in config.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));
                        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));
                config.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size));

                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())
                {
                    try
                    {
                        foreach (LocalFile file in preDeleteFile)
                        {
                            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.Path);
                            if (File.Exists(filePath))
                            {
                                File.Delete(filePath);
                            }

                            config.UpdateFileList.Remove(file);
                        }

                        //StartDownload(downloadList);
                        foreach (DownloadFileInfo file in downloadList)
                        {
                            File.Copy(file.DownloadUrl, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName + ".tmp"));
                            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName);
                            if (File.Exists(filePath))
                            {
                                if (File.Exists(filePath + ".old"))
                                {
                                    File.Delete(filePath + ".old");
                                }

                                File.Move(filePath, filePath + ".old");
                            }

                            File.Move(filePath + ".tmp", filePath);
                        }
                        //更新成功
                        config.SaveConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME));

                        if (bNeedRestart)
                        {
                            MessageBox.Show("自动更新成功,请点击确定重新启动程序。", "自动更新", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Process.Start(Application.ExecutablePath);
                            Environment.Exit(0);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }
        /// <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 (!config.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(config.ServerUrl);

            //获取需要更新的文件列表
            Dictionary <string, RemoteFile> listRemotFile = ParseRemoteXml(strXml);

            //保存需要下载更新的文件
            List <DownloadFileInfo> downloadList = new List <DownloadFileInfo>();

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

            foreach (LocalFile file in config.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));
                        file.LastVer = rf.LastVer;
                        file.Size    = rf.Size;

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

                    //循环一次,删除当前
                    listRemotFile.Remove(file.Path);
                }
                else
                {
                    preDeleteFile.Add(file);
                }
            }


            //这里是如果循环一次以后发现新的DLL加入,则更新本地文件加入新DLL
            foreach (RemoteFile file in listRemotFile.Values)
            {
                downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size));
                config.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size));

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

            if (downloadList.Count > 0)
            {
                DialogResult result = DialogResult.Cancel;

                //如果用户点击过更新但是没有更新完,则强制用户继续更新
                if (config.IsUpdate)
                {
                    result = DialogResult.OK;
                }
                else
                {
                    DownloadConfirm dc = new DownloadConfirm(downloadList);
                    if (this.OnShow != null)
                    {
                        this.OnShow();
                    }
                    result = dc.ShowDialog();
                }

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

                        config.UpdateFileList.Remove(file);
                    }

                    StartDownload(downloadList);
                }
            }
        }