コード例 #1
0
        void updateApp(string appName, string version)
        {
            //下载器
            BRDownloader downloader = new BRDownloader();

            //路径定义
            string remotePackage    = SERVER + "/" + appName + "/packages" + "/" + version + ".zip";
            string cachePath        = startupDirectory + "/cache";
            string localPackage     = cachePath + "\\" + appName + "_" + version + ".zip";
            string extractDirectory = cachePath + "\\" + appName + "\\";

            //检测更新包是否存在
            if (downloader.existFile(remotePackage))
            {
                //检查本地包是否存在,存在则删除
                if (File.Exists(localPackage))
                {
                    File.Delete(localPackage);
                }

                //下载包
                int  count     = 0;
                bool isSuccess = true;
                while (!downloader.downloadFile(localPackage, remotePackage))
                {
                    count++;
                    if (count > maxRetry)
                    {
                        isSuccess = false;
                        break;
                    }
                }
                if (!isSuccess)
                {
                    //下载失败
                    MessageBox.Show("无法获取更新包", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    //下载成功
                    try
                    {
                        if (Directory.Exists(extractDirectory))
                        {
                            Directory.Delete(extractDirectory, true);
                        }
                        FileStream package = new FileStream(localPackage, FileMode.Open);
                        ZipArchive zip     = new ZipArchive(package, ZipArchiveMode.Update);
                        zip.ExtractToDirectory(extractDirectory);
                        package.Close();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        MessageBox.Show("无法解压更新包", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //检查更新脚本
                    if (File.Exists(extractDirectory + "update.exe"))
                    {
                        Process.Start(extractDirectory + "update.exe");
                        Environment.Exit(0);
                    }
                    else
                    {
                        if (File.Exists(extractDirectory + "update.bat"))
                        {
                            WinExec(extractDirectory + "update.bat", 0);
                            Environment.Exit(0);
                        }
                        else
                        {
                            MessageBox.Show("未找到更新程序或脚本", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
                            Directory.Delete(extractDirectory, true);
                            return;
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("无法获取更新包", "更新失败", MessageBoxButton.OK, MessageBoxImage.Error);
                this.Visibility = Visibility.Hidden;
                return;
            }
        }
コード例 #2
0
        void getVerInfo(string appName)
        {
            //创建缓存目录
            string cachePath = startupDirectory + "\\cache";

            if (!Directory.Exists(cachePath))
            {
                Directory.CreateDirectory(cachePath);
            }

            //定义下载器
            BRDownloader downloader       = new BRDownloader();
            string       verFilePath      = SERVER + "/" + appName + "/ver";
            string       localverFilePath = cachePath + "\\" + appName + ".ver";

            //检查文件是否存在
            if (!downloader.existFile(verFilePath))
            {
                //不存在直接返回
                return;
            }

            //检查本地文件是否存在,是则删除
            if (File.Exists(localverFilePath))
            {
                File.Delete(localverFilePath);
            }

            //下载ver文件
            //重试次数
            int  count     = 0;
            bool isSuccess = true;

            while (!downloader.downloadFile(localverFilePath, verFilePath))
            {
                count++;
                if (count > maxRetry)
                {
                    isSuccess = false;
                    break;
                }
            }
            //下载失败
            if (!isSuccess)
            {
                File.Delete(cachePath + "\\" + appName + ".ver");
                checkInternet();
            }
            else
            {
                //下载成功
                IniFile verFile = new IniFile(localverFilePath);
                //解析ver文件
                string remotebuild   = verFile.readValue("ver", "build");
                string remoteversion = verFile.readValue("ver", "version");
                if (remotebuild != "")
                {
                    //是否是更新器的检查
                    if (appName != "br_updater")
                    {
                        //更新应用
                        IniFile appini     = new IniFile(startupDirectory + "\\config.ini");
                        string  localbuild = appini.readValue("app", "build");
                        if (localbuild != "")
                        {
                            if (int.Parse(remotebuild) > int.Parse(localbuild))
                            {
                                if (MessageBox.Show("检测到应用有更新,是否更新?", "更新", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.No)
                                {
                                    return;
                                }
                                updateApp(appName, remoteversion);
                                return;
                            }
                        }
                        else
                        {
                            //版本未配置,退出
                            System.Environment.Exit(0);
                        }
                    }
                    else
                    {
                        //更新更新器
                        if (int.Parse(remotebuild) > build)
                        {
                            if (MessageBox.Show("检测到自动更新器有更新,是否更新?", "更新", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.No)
                            {
                                return;
                            }
                            updateApp(appName, remoteversion);
                            return;
                        }
                    }
                }
            }
        }