Ver.cs 2.8 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Misc
{
    /// <summary>
    /// 给定 程序集其中一个类,需要获取程序集版本号
    /// </summary>
    public class Ver
    {
        /// <summary>
        /// 产品名
        /// </summary>
        public string Product { get; private set; }
        /// <summary>
        /// 版本号
        /// </summary>
        public string V { get; private set; }
        /// <summary>
        /// 编译时间
        /// </summary>
        public string BuildTime { get; private set; }

        public Ver()
        {

        }
        private Type srcType;
        /// <summary>
        /// 需要获取程序集版本号, 程序集其中一个类
        /// </summary>
        public Type SrcType
        {
            get { return srcType; }
            set {
                srcType = value;
                update();
            }
        }
        void update()
        {
            Assembly asm = Assembly.GetAssembly(SrcType);
            AssemblyProductAttribute asmproduct = (AssemblyProductAttribute)Attribute.GetCustomAttribute(asm, typeof(AssemblyProductAttribute));
            Version version = asm.GetName().Version;
            string version_str = "v" + version.Major;
            if (version.Minor != -1) 
            {
                version_str += "." + version.Minor;
            }
            if (version.Build != -1)
            {
                version_str += "." + version.Build;
            }
            if (version.Revision != -1)
            {
                version_str += "." + version.Revision;
            }

            string location = asm.Location;
            string buliddt;
            if (!string.IsNullOrEmpty(location))
            {
                buliddt = System.IO.File.GetLastWriteTime(location).ToString("yyyyMMdd");
            }
            else
            {
                buliddt = "";
            }
            Product = asmproduct.Product;
            V = version_str;
            BuildTime = buliddt;
        }
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(BuildTime))
            {
                return Product + " " + V + " at " + BuildTime;
            }
            else
            {
                return Product + " " + V;
            } 
        }
    }

    public class MainWindowTitle
    {
        public string Title;
        public bool Load()
        {
            try
            {
                string json = File.ReadAllText("title.json");

                Newtonsoft.Json.JsonConvert.PopulateObject(json, this);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}