demo示例:https://gitee.com/chenheze90/L28_AutoVSversion
可通过下载demo运行即可。
原理:通过csproject项目文件中的Target标签,实现在项目编译之前对项目版本号进行修改,避免手动修改;
效果图如下
部分脚本如下:
<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>