using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ryCommon
{
    /// <summary>
    /// 文件信息
    /// </summary>
    public class RySoft
    {
        /// <summary>
        /// 获取版本号
        /// </summary>
        public static string VersionStr
        {
            get
            {
                //Version ver = Assembly.LoadFile(Application.ExecutablePath).GetName().Version;
                //Version ver = Assembly.GetEntryAssembly().GetName().Version;
                //return ver.Major.ToString() + "." + ver.Minor.ToString() + "." + ver.Build.ToString() + "." + ver.;
                return GetVersionStr(Application.ExecutablePath);
            }
        }
        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <param name="exe_path"></param>
        /// <returns></returns>
        public static string GetVersionStr(string exe_path)
        {
            try
            {
                System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(exe_path);
                return info.ProductVersion;
            }
            catch { return ""; }
            //Version ver = Assembly.LoadFile(exe_path).GetName().Version;
            //return ver.Major.ToString() + "." + ver.Minor.ToString() + "." + ver.Build.ToString() + "." + ver.Revision.ToString();
        }
        /// <summary>
        /// 根据版本字符串输出double类型的版本号
        /// </summary>
        /// <param name="ver"></param>
        /// <param name="_softVer"></param>
        /// <param name="_reVer"></param>
        public static void GetVerByVerStr(string ver, out double _softVer, out double _reVer)
        {
            _softVer = 0;
            _reVer = 0;
            string[] ver_item = ver.Split('.');
            switch (ver_item.Length)
            {
                case 1:
                    _softVer = ver_item[0].ToDouble(0);
                    break;
                case 2:
                    _softVer = ver.ToDouble(0);
                    break;
                case 3:
                    _softVer = (ver_item[0] + "." + ver_item[1]).ToDouble(0);
                    _reVer = ver_item[2].ToDouble(0);
                    break;
                case 4:
                    _softVer = (ver_item[0] + "." + ver_item[1]).ToDouble(0);
                    _reVer = (ver_item[2] + "." + ver_item[3]).ToDouble(0);
                    break;
            }
        }
        /// <summary>
        /// 比较两个版本号大小
        /// </summary>
        /// <param name="oldVer"></param>
        /// <param name="NewVer"></param>
        /// <returns>oldVer小于newVer,返回1,大于则返回-1,相等返回0</returns>
        public static int CompareVer(string oldVer,string NewVer)
        {
            GetVerByVerStr(oldVer, out double _old_softVer, out double _old_reVer);
            GetVerByVerStr(NewVer, out double _new_softVer, out double _new_reVer);
            return CompareVer(_old_softVer, _old_reVer, _new_softVer, _new_reVer);
        }
        /// <summary>
        /// 比较版本号
        /// </summary>
        /// <param name="_old_softVer"></param>
        /// <param name="_old_reVer"></param>
        /// <param name="_new_softVer"></param>
        /// <param name="_new_reVer"></param>
        /// <returns>old小于new,返回1,大于则返回-1,相等返回0</returns>
        public static int CompareVer(double _old_softVer,double _old_reVer, double _new_softVer, double _new_reVer)
        {
            if (_old_softVer < _new_softVer) { return 1; }
            if (_old_softVer == _new_softVer)
            {
                if (_old_reVer < _new_reVer) { return 1; }
                else if (_old_reVer == _new_reVer) { return 0; }
                else { return -1; }
            }
            else { return -1; }
        }
        /// <summary>
        /// 判断是否是管理员方式运行
        /// </summary>
        /// <returns></returns>
        public static bool IsAdministrator()
        {
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
            return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
        }
    }
}
