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

如何通过MSBuild脚本读取AssemblyInfo.cs中的Assembly Version与File Version?

Hey there! If you need to pull Assembly Version and File Version directly from your AssemblyInfo.cs using MSBuild, I’ve got a couple of reliable approaches for you—whether you want to read the source file directly or grab the info from a compiled assembly. Let’s break it down!

方法1:直接读取AssemblyInfo.cs(无需编译)

This is perfect if you need the version info before building your project, or if you don’t want to deal with compiled binaries. We’ll read the source file and use regex to extract the version strings.

Here’s a complete MSBuild script snippet you can use:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- Replace this with the actual path to your AssemblyInfo.cs -->
    <AssemblyInfoPath>$(SolutionDir)Properties\AssemblyInfo.cs</AssemblyInfoPath>
  </PropertyGroup>

  <Target Name="ExtractVersionsFromSource">
    <!-- Read all lines from the AssemblyInfo file -->
    <ReadLinesFromFile File="$(AssemblyInfoPath)">
      <Output TaskParameter="Lines" ItemName="AssemblyInfoContent" />
    </ReadLinesFromFile>

    <!-- Grab AssemblyVersion using regex -->
    <PropertyGroup>
      <AssemblyVersionPattern>\[assembly:\s*AssemblyVersion\s*\(\s*"([^"]+)"\s*\)\s*\]</AssemblyVersionPattern>
      <AssemblyVersion>$([System.Text.RegularExpressions.Regex]::Match('@(AssemblyInfoContent)', $(AssemblyVersionPattern)).Groups[1].Value)</AssemblyVersion>
    </PropertyGroup>

    <!-- Grab AssemblyFileVersion the same way -->
    <PropertyGroup>
      <FileVersionPattern>\[assembly:\s*AssemblyFileVersion\s*\(\s*"([^"]+)"\s*\)\s*\]</FileVersionPattern>
      <FileVersion>$([System.Text.RegularExpressions.Regex]::Match('@(AssemblyInfoContent)', $(FileVersionPattern)).Groups[1].Value)</FileVersion>
    </PropertyGroup>

    <!-- Output the results (you can also use these variables in other tasks) -->
    <Message Text="✅ Assembly Version: $(AssemblyVersion)" Importance="high" />
    <Message Text="✅ File Version: $(FileVersion)" Importance="high" />
  </Target>
</Project>

Quick notes:

  • The ReadLinesFromFile task loads the entire AssemblyInfo file into a collection we can work with.
  • We use MSBuild’s built-in regex support to match the version attributes—this ignores extra whitespace or formatting in the file, so it’s pretty robust.
  • If your AssemblyInfo uses AssemblyInformationalVersion too, you can add a similar block for that!
方法2:从编译后的程序集获取版本(更准确)

If your workflow already includes building the project, grabbing versions from the compiled DLL/EXE is more reliable (it avoids any comments or invalid lines in the source file).

Check out this script:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- Update these paths to match your project -->
    <ProjectFilePath>$(SolutionDir)YourProject.csproj</ProjectFilePath>
    <CompiledAssemblyPath>$(SolutionDir)bin\Debug\YourProject.dll</CompiledAssemblyPath>
  </PropertyGroup>

  <Target Name="ExtractVersionsFromCompiledAssembly">
    <!-- Optional: Build the project first if it's not already compiled -->
    <MSBuild Projects="$(ProjectFilePath)" Targets="Build" />

    <!-- Get the Assembly Version (matches AssemblyVersion in AssemblyInfo) -->
    <GetAssemblyIdentity AssemblyFiles="$(CompiledAssemblyPath)">
      <Output TaskParameter="Assemblies" ItemName="TargetAssembly" />
    </GetAssemblyIdentity>

    <!-- Get the File Version (matches AssemblyFileVersion in AssemblyInfo) -->
    <PropertyGroup>
      <AssemblyVersion>%(TargetAssembly.Version)</AssemblyVersion>
      <FileVersion>$([System.Diagnostics.FileVersionInfo]::GetVersionInfo('$(CompiledAssemblyPath)').FileVersion)</FileVersion>
    </PropertyGroup>

    <!-- Output the results -->
    <Message Text="✅ Assembly Version: $(AssemblyVersion)" Importance="high" />
    <Message Text="✅ File Version: $(FileVersion)" Importance="high" />
  </Target>
</Project>

How to run this:

Save either script as VersionExtractor.proj, then run it from the command line:

# For the source file method
msbuild VersionExtractor.proj /t:ExtractVersionsFromSource

# For the compiled assembly method
msbuild VersionExtractor.proj /t:ExtractVersionsFromCompiledAssembly

You can also integrate these targets into your existing .csproj file if you want to run them as part of your regular build process!

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

火山引擎 最新活动