You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Visual Studio 2022中将程序集版本包含到文件夹发布路径中?

解决.NET6 WPF发布时将程序集版本包含到发布路径的问题

我明白你想要的是自动将程序集版本号追加到发布目录路径(比如C:\Code\Publish\MyApplication_1.2.0.28\),之前尝试用自定义VersionGetter类失败是因为MSBuild在发布阶段(编译前)无法访问还未编译的自定义代码。下面是完整的可行方案:

核心思路

我们需要让MSBuild在发布流程中直接获取程序集版本号——可以通过读取你TT模板生成的VersionAutoIncrement.cs文件里的版本信息,或者用GetAssemblyIdentity任务读取编译后的程序集版本。这里推荐第一种,因为不需要等待编译完成,能更早确定发布路径。


步骤1:修改项目文件(.csproj),添加获取版本的MSBuild目标

在你的.csproj文件中添加以下内容,用来从VersionAutoIncrement.cs中提取版本号:

<Target Name="GetAssemblyVersionFromTT" BeforeTargets="ComputeFilesToPublish">
  <!-- 读取TT生成的VersionAutoIncrement.cs文件内容 -->
  <ReadLinesFromFile File="VersionAutoIncrement.cs">
    <Output TaskParameter="Lines" ItemName="VersionFileLines"/>
  </ReadLinesFromFile>

  <!-- 筛选出包含AssemblyVersion的行并提取版本号 -->
  <PropertyGroup>
    <AssemblyVersionLine>$([System.Linq.Enumerable]::FirstOrDefault($(VersionFileLines), x => x.Contains('AssemblyVersion')))</AssemblyVersionLine>
    <AssemblyVersionNumber>$([System.Text.RegularExpressions.Regex]::Match($(AssemblyVersionLine), '"([^"]+)"').Groups[1].Value)</AssemblyVersionNumber>
    <!-- 拼接最终的发布路径 -->
    <PublishDirWithVersion>C:\Code\Publish\MyApplication_$(AssemblyVersionNumber)\</PublishDirWithVersion>
  </PropertyGroup>

  <!-- 可选:输出日志确认版本号是否正确 -->
  <Message Text="Extracted Assembly Version: $(AssemblyVersionNumber)" Importance="High"/>
  <Message Text="Final Publish Directory: $(PublishDirWithVersion)" Importance="High"/>
</Target>

步骤2:修改发布配置文件(FolderProfile.pubxml)

更新你的FolderProfile.pubxml,把PublishDir替换为我们刚才生成的带版本的路径:

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <!-- 使用我们在.csproj中生成的带版本的路径 -->
    <PublishDir>$(PublishDirWithVersion)</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>net6.0-windows</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
  </PropertyGroup>
</Project>

步骤3:验证流程

  1. 确保你的VersionAutoIncrement.tt模板正常生成VersionAutoIncrement.cs文件,包含正确的AssemblyVersion
  2. 右键项目 → 发布 → 选择你的FolderProfile配置
  3. 发布完成后,检查路径是否为C:\Code\Publish\MyApplication_1.2.0.XX\(XX是自动递增的修订号)

备选方案:通过编译后的程序集获取版本

如果你更倾向于从编译好的程序集读取版本,可以修改.csproj中的目标为:

<Target Name="GetAssemblyVersionFromAssembly" BeforeTargets="ComputeFilesToPublish">
  <!-- 指向编译后的程序集路径 -->
  <PropertyGroup>
    <AssemblyPath>$(OutputPath)$(AssemblyName).dll</AssemblyPath>
  </PropertyGroup>

  <!-- 使用GetAssemblyIdentity任务获取版本 -->
  <GetAssemblyIdentity AssemblyFiles="$(AssemblyPath)">
    <Output TaskParameter="Assemblies" ItemName="AssemblyIdentities"/>
  </GetAssemblyIdentity>

  <PropertyGroup>
    <AssemblyVersionNumber>%(AssemblyIdentities.Version)</AssemblyVersionNumber>
    <PublishDirWithVersion>C:\Code\Publish\MyApplication_$(AssemblyVersionNumber)\</PublishDirWithVersion>
  </PropertyGroup>

  <Message Text="Assembly Version from DLL: $(AssemblyVersionNumber)" Importance="High"/>
</Target>

这个方案需要确保程序集已经编译完成,所以目标的BeforeTargets要设置为ComputeFilesToPublish(这个阶段程序集已经编译好)。


为什么之前的方法不行?

当你在pubxml中使用$([VersionGetter]::GetVersion())时,MSBuild会尝试调用一个在MSBuild上下文中存在的类型,但你的VersionGetter是项目编译后的代码,在发布流程的早期阶段还不存在,所以MSBuild无法识别这个类型,导致触发新建配置文件向导。而System.Guid是MSBuild内置可以访问的.NET类型,所以能正常工作。

内容的提问来源于stack exchange,提问作者SvendK

火山引擎 最新活动