demo示例:https://gitee.com/chenheze90/L28_AutoVSversion
可通过下载demo运行即可。
原理:通过csproject项目文件中的Target标签,实现在项目编译之前对项目版本号进行修改,避免手动修改;
1.基础版
效果图如下
部分脚本如下:
<Project>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
<Target Name="SetAssemblyVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<Year>$([System.DateTime]::Now.ToString("yy"))</Year>
<MonthDay>$([System.DateTime]::Now.ToString("MMdd"))</MonthDay>
</PropertyGroup>
<Message Text="Setting AssemblyVersion to 1.0.$(Year).$(MonthDay)" Importance="high" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="//using System.Reflection;" Overwrite="true" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyTitle("MyAPPTitle")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyDescription("2021.03.29")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyConfiguration("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCompany("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyProduct("MyPro")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCopyright("Copyright ? CCC")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyTrademark("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCulture("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Runtime.InteropServices.ComVisible(false)]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Windows.ThemeInfo(System.Windows.ResourceDictionaryLocation.None, System.Windows.ResourceDictionaryLocation.SourceAssembly)]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyVersion("01.00.00$(Year).$(MonthDay)")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyFileVersion("01.00.00$(Year).$(MonthDay)")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyInformationalVersion("01.00.00$(Year).$(MonthDay)")]" Overwrite="false" />
</Target>
</Project>
2.进阶版
部分项目要自定义生成版本号,可通过自定义类的方式来实现。
1.新建项目ClassLibrary1
2.引用系统类库sing Microsoft.Build.Framework;;using Microsoft.Build.Utilities;
3.新建类GenerateVersionTask
public class GenerateVersionTask : Task
{
[Output]
public int Version { get; set; }
public override bool Execute()
{
// 生成版本号的逻辑
Version = GetVisitCount();
return true;
}
private const string DataFilePath = "visit_counter.dat";
public static int GetVisitCount()
{
// 读取存储的数据
int visitCount = 0; DateTime lastVisitDate = DateTime.Now;
ReadData(ref visitCount, ref lastVisitDate);
// 获取当前日期
DateTime today = DateTime.Today;
// 检查是否是新的一天
if (lastVisitDate < today)
{
// 重置访问次数
visitCount = 0;
lastVisitDate = today;
}
// 增加访问次数
visitCount++;
// 保存数据
SaveData(visitCount, lastVisitDate);
// 返回访问次数
return visitCount;
}
private static void ReadData(ref int count, ref DateTime countdate)
{
if (File.Exists(DataFilePath))
{
string[] lines = File.ReadAllLines(DataFilePath);
if (lines.Length == 2)
{
int visitCount = int.Parse(lines[0]);
DateTime lastVisitDate = DateTime.Parse(lines[1]);
count = visitCount; countdate = lastVisitDate;
}
}
else
{
File.Create(DataFilePath);
count = 0; countdate = DateTime.Now;
}
}
private static void SaveData(int visitCount, DateTime lastVisitDate)
{
string[] lines = { visitCount.ToString(), lastVisitDate.ToString() };
File.WriteAllLines(DataFilePath, lines);
}
}
4.增加脚本
<UsingTask TaskName="GenerateVersionTask" AssemblyFile="$(TargetDir)\ClassLibrary1.dll" />
和
<GenerateVersionTask>
<Output TaskParameter="Version" PropertyName="MyVersion" />
</GenerateVersionTask>
效果如图所示
<UsingTask TaskName="GenerateVersionTask" AssemblyFile="$(TargetDir)\ClassLibrary1.dll" />
<Target Name="SetAssemblyVersion" BeforeTargets="BeforeBuild">
<PropertyGroup>
<Year>$([System.DateTime]::Now.ToString("yy"))</Year>
<MonthDay>$([System.DateTime]::Now.ToString("MMdd"))</MonthDay>
</PropertyGroup>
<GenerateVersionTask>
<Output TaskParameter="Version" PropertyName="MyVersion" />
</GenerateVersionTask>
<Message Text="Generated Version: $(MyVersion)" Importance="high" />
<Message Text="Setting AssemblyVersion to 01.0$(MyVersion).00$(Year).$(MonthDay)" Importance="high" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="//using System.Reflection;" Overwrite="true" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyTitle("MyAPPTitle")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyDescription("2021.03.29")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyConfiguration("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCompany("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyProduct("MyPro")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCopyright("Copyright ? CCC")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyTrademark("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCulture("")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Runtime.InteropServices.ComVisible(false)]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Windows.ThemeInfo(System.Windows.ResourceDictionaryLocation.None, System.Windows.ResourceDictionaryLocation.SourceAssembly)]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyVersion("01.0$(MyVersion).00$(Year).$(MonthDay)")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyFileVersion("01.0$(MyVersion).00$(Year).$(MonthDay)")]" Overwrite="false" />
<WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyInformationalVersion("01.0$(MyVersion).00$(Year).$(MonthDay)")]" Overwrite="false" />
</Target>
</Project>