c# 获取程序集AssemblyVersion 版本号

 

//获取程序Properties文件夹下面的AssemblyInfo.cs文件, [assembly: AssemblyVersion("1.0.0.1")]

 

public string GetVersion()
{
System.Reflection.Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
System.Reflection.AssemblyName assemblyName = null;
foreach (System.Reflection.Assembly assembly in assemblies)
{
// 根据需要筛选程序集,例如通过程序集名称
if (assembly.FullName.StartsWith("Project.Web"))
{
assemblyName = assembly.GetName();
break;
}
}

//string version = assemblyName.Version.ToString(); // 这种方法2024.04.05.01 会被类型转换成int, 结果为 2024.4.5.1
//例如:2024.4.5.1 后面三位都会被转换成两位数,结果为 2024.04.05.01
string version = $"{assemblyName.Version.Major}.{assemblyName.Version.Minor:D2}.{assemblyName.Version.Build:D2}.{assemblyName.Version.Revision:D2}";
if (version.EndsWith(".0"))
{
version = version.Substring(0, version.Length - 2);
}
return version;
}

 

posted @ 2024-04-01 11:48  二零一七  阅读(307)  评论(0编辑  收藏  举报