using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Misc { /// /// 给定 程序集其中一个类,需要获取程序集版本号 /// public class Ver { /// /// 产品名 /// public string Product { get; private set; } /// /// 版本号 /// public string V { get; private set; } /// /// 编译时间 /// public string BuildTime { get; private set; } public Ver() { } private Type srcType; /// /// 需要获取程序集版本号, 程序集其中一个类 /// 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 = string.Format("v{0}.{1}.{2}", version.Major, version.Minor, version.Build); 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; } } } }