2

I am using VS2022 17.2.3.

In a .NET Standard 2.0 Library Project I wish to change the output assembly file name.

In Project -> Properties -> Assembly Name, I see that it is set to $(MSBuildProjectName).

I want to change it to $(MSBuildProjectName)$(AssemblyVersion), but $(AssemblyVersion) is not working.

I want to ask if anyone can point to right thing.

1
  • May I know if you have got any chance to check the answer below? If it works you could click '✔' to mark it as an answer to change its status to Answered. It will also help others to solve a similar issue. Commented Jun 9, 2022 at 7:41

1 Answer 1

5

This is due to MSBuild's Property Evaluation Order.

Setting AssemblyVersion in the csproj file before setting AssemblyName works fine:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <AssemblyName>$(MSBuildProjectName)$(AssemblyVersion)</AssemblyName>
  </PropertyGroup>
</Project>
1>CSharpScratchpad -> C:\Projects\CSharpScratchpad\bin\Debug\net6.0\CSharpScratchpad1.0.0.0.dll

In the comments, you also stated you want to use a wildcard AssemblyVersion, like 1.0.*.

I don't know a good way to access the expanded, final form of the version MSBuild generates internally, so I can only offer a slightly ugly post build copy retrieving the version from the built assembly:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.0.*</AssemblyVersion>
    <Deterministic>false</Deterministic>
  </PropertyGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="AssemblyIdentity"/>
    </GetAssemblyIdentity>
    <Exec Command='COPY "$(TargetPath)" "$(TargetDir)$(TargetName)%(AssemblyIdentity.Version)$(TargetExt)" /Y' />
  </Target>
</Project>

In a follow-up comment, you wanted to only append the major.minor version to the file name.

You can create a System.Version instance of the assembly version, and call its ToString(int fieldCount) method so that it only returns the first 2 segments. This would be the new post build target - I've stored the result in a MajorMinor property for sanity readability, but you can bash it all into one line if you prefer:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="AssemblyIdentity"/>
  </GetAssemblyIdentity>
  <PropertyGroup>
    <MajorMinor>$([System.Version]::new("%(AssemblyIdentity.Version)").ToString(2))</MajorMinor>
  </PropertyGroup>
  <Exec Command='COPY "$(TargetPath)" "$(TargetDir)$(TargetName)$(MajorMinor)$(TargetExt)" /Y' />
</Target>

I suggest being able to explain this to your collegues or yourself in a few months. And maybe someone else knows a less convoluted solution for this.

Sign up to request clarification or add additional context in comments.

5 Comments

<Deterministic>false</Deterministic> <AssemblyVersion>1.0.*</AssemblyVersion> <AssemblyName>$(MSBuildProjectName)$(AssemblyVersion)</AssemblyName> Changing 1.0.0.0 to 1.0.* does not work.
@Neha Please don't use the term "does not work" as it does not help identifying your problem. That said, I tried your setup, and your problem apparently is that MSBuild does not expand AssemblyVersion and attempts to use the * wildcard in the assembly file name. I don't know how to expand AssemblyVersion after MSBuild applied its internal logic on it, but I've provided a rather ugly post build rename "solution".
The PostBuild Idea works. It gives output like this: Library1.0.8195.31489.dll
while I am happy with the output result, I was wondering if i can use only Major and Minor from AssemblyIdentity.Version. I tried entering AssemblyIdentity.Version.Major but it copies dll file as Library(AssemblyIdentity.Version.Major).dll.
@Neha I added a way to do that. If you like it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.