Skip to content

Instantly share code, notes, and snippets.

@mcxiaoke
Created March 18, 2026 12:11
Show Gist options
  • Select an option

  • Save mcxiaoke/7c8171075fae29bebafd91ae1345e358 to your computer and use it in GitHub Desktop.

Select an option

Save mcxiaoke/7c8171075fae29bebafd91ae1345e358 to your computer and use it in GitHub Desktop.
visual studio 2019/2022/2026 auto version using git commit hash and date time and auto increment config sample csproj file 自动版本号使用日期时间和Git提交Hash值

使用方法(超级简单)

把上面整个内容完整覆盖你的 .csproj 文件(备份一下原文件)。 清理 + 重建(Clean + Rebuild Solution)。 构建后效果: Release exe 属性 → 文件版本 = 1.0.123.1452(天数会每天增大) Release 版本号 = 1.0.0-20260318-abc1234 Debug 版本号 = 1.0.0-debug 输出窗口会看到清晰的 === [编译信息] ... ===

以后维护:

大版本升级 → 只改 和 、 想换基准年 → 只改 想精确到秒 → 告诉我,我再给你加 HHmmss 版本

这个文件已经把我们前面所有讨论一次性整合成最完整、最干净、可直接使用的版本。 直接保存为 YourProject.csproj 就能用,留着当备忘录完美! 需要我再加 AssemblyVersion 固定为 1.0.0.0、或者加上 GitVersion 自动 bump、或者改成多项目解决方案的 Directory.Build.props 版,随时说~

<Project Sdk="Microsoft.NET.Sdk">
<!-- ================================================ -->
<!-- ================ 项目基本设置 ================= -->
<!-- ================================================ -->
<PropertyGroup>
<!-- 项目类型(控制台应用示例,你可以改成 WinExe / Library 等) -->
<OutputType>Exe</OutputType>
<!-- .NET 版本(VS 2026 默认支持 .NET 9 / .NET 10,这里用 .NET 9 示例,改成 net10.0 也行) -->
<TargetFramework>net9.0</TargetFramework>
<!-- 启用最新语言特性 -->
<LangVersion>latest</LangVersion>
<!-- 可空引用(推荐开启) -->
<Nullable>enable</Nullable>
<!-- 隐式 using(推荐) -->
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<!-- ================================================ -->
<!-- ================ 版本号核心配置 ================ -->
<!-- ================================================ -->
<PropertyGroup>
<!--
手动维护的基础版本(大版本升级时只改这里)
格式:Major.Minor.Patch(Patch 会被我们后面自动覆盖为后缀)
-->
<VersionPrefix>1.0.0</VersionPrefix>
<!-- FileVersion 前两段(以后升级主/次版本时,这里和 VersionPrefix 要同步修改) -->
<Major>1</Major>
<Minor>0</Minor>
<!--
FileVersion 日期基准年(关键!防止跨年回滚)
推荐新项目设为项目启动年(如 2025-01-01),这样天数从 0 开始,数字更小
老项目保持 2000-01-01 即可
-->
<FileVersionEpoch>2025-01-01</FileVersionEpoch>
</PropertyGroup>
<!-- ================================================ -->
<!-- ================ 自定义版本生成逻辑 ============ -->
<!-- ================================================ -->
<!--
这个 Target 在每次构建前自动运行
BeforeTargets 包含了 AssemblyVersion / NuGet / PackageVersion 所需的所有关键节点
支持 Debug / Release 不同后缀 + Git + 日期时间
-->
<Target Name="GenerateCustomVersion"
BeforeTargets="GetAssemblyVersion;GenerateNuspec;GetPackageVersion">
<!-- 1. 获取 Git short commit(7位) -->
<!-- 如果不在 Git 仓库或 git 命令失败,会自动变成 "unknown" -->
<Exec Command="git rev-parse --short HEAD"
ConsoleToMSBuild="true"
StandardOutputImportance="low"
IgnoreExitCode="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GitCommitHash" />
</Exec>
<!-- 2. 计算构建时间(精确到分钟) + 跨年安全的“自基准年以来第几天” -->
<PropertyGroup>
<!-- 用于 Release 版本后缀的完整日期 -->
<BuildDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</BuildDate>
<!-- 用于 FileVersion 的时间(HHmm) -->
<BuildTime>$([System.DateTime]::Now.ToString('HHmm'))</BuildTime>
<!--
关键计算:自 FileVersionEpoch 以来经过的天数(每天 +1)
跨年永远递增,彻底解决“明年1月比今年3月小”的问题
到 2100 年也才约 36,500(远小于 65,535 限制)
-->
<DaysSinceEpoch>$([System.DateTime]::Now.Subtract($([System.DateTime]::Parse('$(FileVersionEpoch)'))).Days)</DaysSinceEpoch>
<!-- Git 失败时的默认值 -->
<GitCommitHash Condition="'$(GitCommitHash)' == ''">unknown</GitCommitHash>
</PropertyGroup>
<!-- 3. Debug 配置:直接加 debug 后缀 -->
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<VersionSuffix>debug</VersionSuffix>
</PropertyGroup>
<!-- 4. Release 配置:日期 + Git short commit 后缀(符合你最初需求) -->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<VersionSuffix>$(BuildDate)-$(GitCommitHash)</VersionSuffix>
</PropertyGroup>
<!-- 5. 最终版本号设置(NuGet / Assembly / InformationalVersion 都用这个) -->
<PropertyGroup>
<Version>$(VersionPrefix)-$(VersionSuffix)</Version>
<InformationalVersion>$(Version)</InformationalVersion>
</PropertyGroup>
<!-- 6. FileVersion(重点修复版):1.0.天数.HHmm -->
<!-- 永远单调递增,Windows 文件版本比较完全正确 -->
<PropertyGroup>
<FileVersion>$(Major).$(Minor).$(DaysSinceEpoch).$(BuildTime)</FileVersion>
</PropertyGroup>
<!-- 7. 构建时在输出窗口高亮显示(方便你调试) -->
<Message Importance="high"
Text="=== [编译信息] 干净版本: $(Version) | FileVersion: $(FileVersion) (天数=$(DaysSinceEpoch)) ===" />
</Target>
<!-- ================================================ -->
<!-- ================ 其他常用配置(可选) ========== -->
<!-- ================================================ -->
<!--
如果你打包 NuGet 包,建议加上下面这些(可删除)
-->
<!--
<PropertyGroup>
<PackageId>MyCompany.MyApp</PackageId>
<Authors>你的名字</Authors>
<Description>带 Git+日期版本号的示例项目</Description>
<PackageVersion>$(Version)</PackageVersion>
</PropertyGroup>
-->
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment