CheckVersion.cs 8.7 KB
using System;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using BizFacade;

namespace App
{
    /// <summary>
    /// CheckVersion 的摘要说明。

    /// </summary>
    public class CheckVersion
    {
        private string _LocalMainVersion;
        private string _LocalMainPublicKey;
        private string _LocalMainCulture;
        private string _RemoteMainVersion;
        private string _RemoteMainPublicKey;
        private string _RemoteMainCulture;
        private string _UpdateFileUrl;
        private string _CheckVersionExeLocalPath;
        private bool _CanEscaladeClient = true;
        private bool _SkipCheck = false;

        /// <summary>
        /// CheckVersion初始化函数

        /// </summary>
        public CheckVersion()
        {
            CheckUpdateSwitch();
            Initialize();
        }

        /// <summary>
        /// 开始版本比对入口函数

        /// </summary>
        public void StartCheck()
        {
            //			GetUpdateFileUrl();
            GetRemotingMainVersion();
        }

        /// <summary>
        /// 获取运行时程序自身的版本,地区,公钥标识.
        /// </summary>
        private void Initialize()
        {
            try
            {
                Assembly Asm = Assembly.LoadFrom("App.exe");
                AssemblyName AsmName = Asm.GetName();
                this._LocalMainVersion = AsmName.Version.ToString();
                this._LocalMainCulture = AsmName.CultureInfo.ToString();
                this._LocalMainPublicKey = Convert.ToString(AsmName.GetPublicKeyToken());
            }
            catch
            {
                throw new Exception();
            }
        }


        /// <summary>
        /// 获取远端Buggit Client 版本
        /// </summary>
        /// <returns>版本</returns>
        public string GetRemotingMainVersion()
        {
            try
            {
                UpdateSystem upws = new UpdateSystem();
                this._RemoteMainVersion = upws.GetClientVersion();
            }
            catch (Exception e)
            {
                throw new Exception("无法连接到升级服务器!\n(" + e.Message + ")\n");
            }

            return this._RemoteMainVersion;
        }

        /// <summary>
        /// 对本地和远端版本进行比较
        /// </summary>
        /// <returns>版本是否相符 0 为相符,1 为更新,2 为更旧</returns>
        public int CompareVersion()
        {
            string f_LocalVersion = this._LocalMainVersion;
            string f_RemoteVersion = this._RemoteMainVersion;
            int f_LocalNum;
            int f_RemoteNum;
            if (f_LocalVersion == f_RemoteVersion)
            {
                return 0;
            }
            int i;
            for (i = 0; i < 4; i++)
            {
                if (f_LocalVersion.LastIndexOf(@".") > 0)
                {
                    f_LocalNum = int.Parse(f_LocalVersion.Substring(0, f_LocalVersion.IndexOf(@".")));
                    f_RemoteNum = int.Parse(f_RemoteVersion.Substring(0, f_RemoteVersion.IndexOf(@".")));
                }
                else
                {
                    f_LocalNum = int.Parse(f_LocalVersion);
                    f_RemoteNum = int.Parse(f_RemoteVersion);
                }
                if (f_LocalNum != f_RemoteNum)
                {
                    if (f_LocalNum < f_RemoteNum)
                    {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
                f_LocalVersion = f_LocalVersion.Substring(f_LocalVersion.IndexOf(@".") + 1);
                f_RemoteVersion = f_RemoteVersion.Substring(f_RemoteVersion.IndexOf(@".") + 1);
            }
            return 0;
        }

        /// <summary>
        /// 调用CheckVersion.exe
        /// </summary>
        public void CallUpdateFile()
        {
            string ErrorMsg = "";
            try
            {
                string strTemp = Application.ExecutablePath;
                strTemp = strTemp.Substring(0, strTemp.LastIndexOf("\\") + 1);
                this._CheckVersionExeLocalPath = strTemp;
                FileInfo fileinfo = new FileInfo(this._CheckVersionExeLocalPath + @"\LiveUpdate.exe");
                if (!fileinfo.Exists)
                {
                    ErrorMsg = "在当前目录找不到LiveUpdate.exe升级程序!\n";
                }
                Process myProcess = new Process();
                myProcess.StartInfo.FileName = this._CheckVersionExeLocalPath + @"\LiveUpdate.exe";
                // 传入"-S"的参数表示启动LiveUpdate.exe后,可自动开始升级过程

                myProcess.StartInfo.Arguments = "-S";
                myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                myProcess.Start();

            }
            catch (Exception e)
            {
                throw new Exception(ErrorMsg + e.Message);
            }

        }

        private void CheckUpdateSwitch()
        {
            string f_UpdateSwitch = "";
            try
            {
                UpdateSystem upws = new UpdateSystem();
                f_UpdateSwitch = upws.GetUpdateSwitch();
            }
            catch
            {
                f_UpdateSwitch = "";
            }
            if (f_UpdateSwitch == "True")
            {
                this._SkipCheck = true;
            }
            else
            {
                this._SkipCheck = false;
            }
        }

        /// <summary>
        /// 当前程序主版本

        /// </summary>
        public string LocalMainVersion
        {
            get
            {
                return _LocalMainVersion;
            }
            set
            {
                _LocalMainVersion = value;
            }
        }
        /// <summary>
        ///  当前程序公钥标识
        /// </summary>
        public string LocalMainPublicKey
        {
            get
            {
                return _LocalMainPublicKey;
            }
            set
            {
                _LocalMainPublicKey = value;
            }
        }
        /// <summary>
        /// 当前程序地区标识
        /// </summary>
        public string LocalMainCulture
        {
            get
            {
                return _LocalMainCulture;
            }
            set
            {
                _LocalMainCulture = value;
            }
        }
        /// <summary>
        /// 远端程序主版本

        /// </summary>
        public string RemoteMainVersion
        {
            get
            {
                return _RemoteMainVersion;
            }
            set
            {
                _RemoteMainVersion = value;
            }
        }
        /// <summary>
        /// 远端主程序公钥标识

        /// </summary>
        public string RemoteMainPublicKey
        {
            get
            {
                return _RemoteMainPublicKey;
            }
            set
            {
                _RemoteMainPublicKey = value;
            }
        }
        /// <summary>
        /// 远端程序地区标识
        /// </summary>
        public string RemoteMainCulture
        {
            get
            {
                return _RemoteMainCulture;
            }
            set
            {
                _RemoteMainCulture = value;
            }
        }

        /// <summary>
        /// 更新的Buggit Client URL地址
        /// </summary>
        public string UpdateFileUrl
        {
            get
            {
                return _UpdateFileUrl;
            }
            set
            {
                _UpdateFileUrl = value;
            }
        }

        /// <summary>
        /// 更新程序的本地相对路径

        /// </summary>
        public string CheckVersionExeLocalPath
        {
            get
            {
                return _CheckVersionExeLocalPath;
            }
            set
            {
                _CheckVersionExeLocalPath = value;
            }
        }

        /// <summary>
        /// 是否继续运行Buggit Client标志位

        /// </summary>
        public bool CanEscaladeClient
        {
            get
            {
                return _CanEscaladeClient;
            }
            set
            {
                _CanEscaladeClient = value;
            }
        }

        /// <summary>
        /// 是否略过版本检查

        /// </summary>
        public bool SkipCheck
        {
            get
            {
                return _SkipCheck;
            }
            set
            {
                _SkipCheck = value;
            }
        }

    }

}