Exemple #1
0
        /// <summary>
        /// 获取本地版本号
        /// </summary>
        /// <returns></returns>
        VersionInfoResponse UpdateVersion_GetLocalVersion()
        {
            VersionInfoResponse localVersionInfo = new VersionInfoResponse();

            try
            {
                if (!File.Exists(localVersionFilePath))
                {
                    //写入空文件即可
                    File.WriteAllText(localVersionFilePath, "");
                    return(localVersionInfo);
                }
                else
                {
                    String versionTxt = File.ReadAllText(localVersionFilePath, Encoding.UTF8);
                    if (!String.IsNullOrEmpty(versionTxt))
                    {
                        localVersionInfo = JsonConvert.DeserializeObject <VersionInfoResponse>(versionTxt);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(localVersionInfo);
        }
Exemple #2
0
 void UpdateVersion()
 {
     try
     {
         /**
          * 1.获取服务器版本号
          * 2.所有文件存储至本地目录
          */
         //如果目录不存在,则重新创建目录
         if (!Directory.Exists(updateDic))
         {
             Directory.CreateDirectory(updateDic);
         }
         VersionInfoResponse localVersion  = UpdateVersion_GetLocalVersion();
         VersionInfoResponse serverVersion = UpdateVersion_GetServerVersion();
         double serverV = UpdateVersion_ConvertVersionVal(serverVersion);
         double localV  = UpdateVersion_ConvertVersionVal(localVersion);
         Log(String.Format("服务器版本为:{0},本地版本为:{1}", serverV, localV));
         if (serverV > localV)
         {
             //如服务器端版本信息大于本地,则从服务端下载最新版本,并更新本地信息
             bool downLoadSign = UpdateVersion_DownloadFromServer(serverVersion);
             if (downLoadSign)
             {
                 //下载成功后,将最新版本信息更新至本地,并设置为未处理
                 serverVersion.updateSign      = "1";
                 serverVersion.mainProcessPath = basePath;
                 serverVersion.mainProcessName = "ChinaLifeTools";
                 //覆盖到本地,提醒客户更新
                 File.WriteAllText(localVersionFilePath, JsonConvert.SerializeObject(serverVersion));
                 this.versionLabel.Text = uptTips;
             }
         }
         else
         {
             if (localV > 0)
             {
                 if ("1".Equals(localVersion.updateSign))
                 {
                     this.versionLabel.Text = uptTips;
                 }
                 else if ("0".Equals(localVersion.updateSign))
                 {
                     //清除提示
                     Log("清除版本号提醒");
                     this.versionLabel.Text = "";
                 }
             }
         }
     }
     catch (Exception ex)
     {
     }
 }
Exemple #3
0
        /// <summary>
        /// 1.更新程序
        /// </summary>
        public void UpdateFile(String receiveMsg)
        {
            try
            {
                if (!String.IsNullOrEmpty(receiveMsg))
                {
                    //1.反序列化
                    VersionInfoResponse updateInfo = JsonConvert.DeserializeObject <VersionInfoResponse>(receiveMsg);
                    if (updateInfo != null && "1".Equals(updateInfo.updateSign) && File.Exists(updateInfo.downloadFilePath))
                    {
                        //1.关闭进程
                        UpdateFile_CloseSameProcess(updateInfo.mainProcessName);

                        //2.直接解压直新
                        //String depressPath = updateInfo.downloadFilePath.Substring(0, updateInfo.downloadFilePath.LastIndexOf('.'));
                        //bool depressStatus = ZipUtils.Decompression(updateInfo.downloadFilePath, updateInfo.mainProcessPath, true);
                        bool depressStatus = UpdateFile_CopyFile(updateInfo);
                        if (depressStatus)
                        {
                            File.Delete(updateInfo.downloadFilePath);
                            //4.更新本地最新版本号
                            updateInfo.updateSign = "0";
                            String updateVersionFilePath = String.Format("{0}\\update\\version.txt", updateInfo.mainProcessPath);
                            File.WriteAllText(updateVersionFilePath, JsonConvert.SerializeObject(updateInfo));
                        }
                        else
                        {
                            MessageBox.Show("解压缩失败,请稍后重试");
                        }


                        //5.启动文件
                        String mainProcess = String.Format("{0}\\{1}.exe", updateInfo.mainProcessPath, updateInfo.mainProcessName);
                        Process.Start(mainProcess);

                        //6.可退出更新程序,也可不退出(考虑socket连接不生效)
                        System.Environment.Exit(0);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("更新失败,请联系管理员或者重新下载即可!!!:{0}", ex.Message));
                System.Environment.Exit(0);
            }
        }
Exemple #4
0
        /// <summary>
        /// 转换版本大小
        /// </summary>
        /// <param name="versionInfo"></param>
        /// <returns></returns>
        double UpdateVersion_ConvertVersionVal(VersionInfoResponse versionInfo)
        {
            double versionDouble = 0;

            try
            {
                if (versionInfo != null && versionInfo.data != null && !String.IsNullOrEmpty(versionInfo.data.versionNumber))
                {
                    double tmp;
                    if (double.TryParse(versionInfo.data.versionNumber, out tmp))
                    {
                        versionDouble = tmp;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(versionDouble);
        }
Exemple #5
0
        /// <summary>
        /// 获取服务端版本号
        /// </summary>
        /// <returns></returns>
        VersionInfoResponse UpdateVersion_GetServerVersion()
        {
            VersionInfoResponse serverVersionInfo = new VersionInfoResponse();

            try
            {
                /*
                 * 1.从服务端查询出版本号信息
                 */
                String serverVersionUrl = Config.ServerVersionUrl;
                String serverResult     = HttpUtils.Get(serverVersionUrl, null);
                if (!String.IsNullOrEmpty(serverResult))
                {
                    serverVersionInfo = JsonConvert.DeserializeObject <VersionInfoResponse>(serverResult);
                }
            }
            catch (Exception ex)
            { }
            return(serverVersionInfo);
        }
Exemple #6
0
        /// <summary>
        /// 从服务端下载更新库
        /// </summary>
        /// <param name="serverVersionInfo"></param>
        /// <returns></returns>
        bool UpdateVersion_DownloadFromServer(VersionInfoResponse serverVersionInfo)
        {
            bool downloadSign = false;

            try
            {
                if (serverVersionInfo != null && serverVersionInfo.data != null && serverVersionInfo.data.downLoadUrl != null)
                {
                    //从服务端下载
                    String downloadFilePath = String.Format("{0}\\{1}", updateDic, serverVersionInfo.data.fileName);
                    HttpUtils.Get_SaveToLocalPath(serverVersionInfo.data.downLoadUrl, null, downloadFilePath);
                    if (File.Exists(downloadFilePath))
                    {
                        serverVersionInfo.downloadFilePath = downloadFilePath;
                        downloadSign = true;
                    }
                }
            }
            catch (Exception) { }
            return(downloadSign);
        }
Exemple #7
0
        /// <summary>
        /// 解压文件,并处理
        /// </summary>
        /// <returns></returns>
        bool UpdateFile_CopyFile(VersionInfoResponse updateInfo)
        {
            bool status = false;

            try
            {
                /*
                 * 1.先解压缩文件到tmp路径
                 * 2.从tmp路径下读取出非update目录下的文件,拷贝到代理工具执行目录
                 * 3.删除tmp目录,
                 */
                String unRarTmpDic = String.Format(@"{0}\update\tmp", updateInfo.mainProcessPath);
                if (!Directory.Exists(unRarTmpDic))
                {
                    Directory.CreateDirectory(unRarTmpDic);
                }
                RarOrZipUtils.UnpackFileRarOrZip(updateInfo.downloadFilePath, unRarTmpDic);

                var files = Directory.GetFiles(unRarTmpDic);
                foreach (var item in files)
                {
                    FileInfo fInfo       = new FileInfo(item);
                    String   desFilePath = String.Format(@"{0}\{1}", updateInfo.mainProcessPath, fInfo.Name);
                    fInfo.CopyTo(desFilePath, true);
                    fInfo.Delete();
                }
                Directory.Delete(unRarTmpDic, true);
                File.Delete(updateInfo.downloadFilePath);

                status = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(status);
        }