VS项目,在生成的时候自动修改版本号

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(&quot;MyAPPTitle&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyDescription(&quot;2021.03.29&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyConfiguration(&quot;&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCompany(&quot;&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyProduct(&quot;MyPro&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCopyright(&quot;Copyright ?  CCC&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyTrademark(&quot;&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyCulture(&quot;&quot;)]" 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(&quot;01.00.00$(Year).$(MonthDay)&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyFileVersion(&quot;01.00.00$(Year).$(MonthDay)&quot;)]" Overwrite="false" />
    <WriteLinesToFile File="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" Lines="[assembly: System.Reflection.AssemblyInformationalVersion(&quot;01.00.00$(Year).$(MonthDay)&quot;)]" Overwrite="false" />
  </Target>
</Project>
相关推荐
爱进步2 小时前
离线安装Visual Studio 2022
ide·visual studio
weixin_497845544 小时前
Visual Studio 、 MSBuild 、 Roslyn 、 .NET Runtime、SDK Tools之间的关系
ide·.net·visual studio
ZhangChuChu_924819 小时前
VS2022(Visual Studio)中显示行数(c#)
ide·c#·visual studio
无情时尚1 天前
VisualStudio vsix插件自动加载
ide·visual studio
就叫飞六吧2 天前
Electron和C/C++开发桌面应用对比
c语言·开发语言·c++·visual studio
哆啦A梦是一只狸猫2 天前
【已解决】启动此实时调试器时未使用必需的安全权限。要调试该进程,必须以管理员身份运行此实时调试器。是否调试该进程?
安全·visual studio
Jambo!2 天前
Visual studio中C/C++连接mysql
c语言·c++·mysql·visual studio
码农老起2 天前
常用代码开发工具技术分享
git·docker·github·visual studio code·visual studio
羊小猪~~2 天前
数据结构C语言描述7(图文结合)--串的实现与BP算法、KMP算法讲解与模版提供
c语言·数据结构·c++·vscode·考研·算法·visual studio