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>
相关推荐
EQ-雪梨蛋花汤2 天前
【工具】在 Visual Studio 中使用 Dotfuscator 对“C# 类库(DLL)或应用程序(EXE)”进行混淆
数据库·ide·visual studio
MessiGo3 天前
Visual Studio | 性能探测器
ide·visual studio
代码雕刻家6 天前
Visual Studio 2022中打开其他项目的步骤
ide·visual studio
1YaoWei06 天前
VSIX 加密存取数据的案例
visual studio
工藤新一OL6 天前
.netCore的winform程序如何调用webapi
c#·.net·.netcore·visual studio
yy_xzz8 天前
Visual Studio 2022静态库与动态库创建及使用完全指南
c++·visual studio
三体世界8 天前
C++ STL 序列式容器之(三)-- List
java·c语言·开发语言·c++·windows·list·visual studio
初级代码游戏9 天前
管理Visual Studio配置文件(使用Azure DevOps开发,免费GIT托管)
git·azure·devops·visual studio
珊瑚里的鱼9 天前
第三讲 | C/C++内存管理完全手册
c语言·c++·笔记·程序人生·visualstudio·visual studio
成功助力英语中国话9 天前
visual studion 2022如何使用PlaySound()
visual studio·win32·playsound