Search code examples
c#visual-studioversioning

Getting unexpected ApplicationVersion and ProductVersion in Visual Studio project


A project I'm currently working on in Visual Studio 2022 was originally written for .NET 6 and I have upgraded it to .NET 8.0. Previously the result from Application.ProductVersion gave the expected result as per the value of [assembly: AssemblyFileVersion("0.4.2")] and [assembly: AssemblyVersion("0.4.2")] in the AssemblyInfo.cs file in the original project.

Now after upgrading there is no longer any AssemblyInfo.cs file in the project. It is my understanding that when a project uses the <Project Sdk="Microsoft.NET.Sdk"> line in the .csproj file, the settings should be migrated to this file and AssemblyInfo.cs is no longer used. There are the following lines in my Project.csproj file:

    <ApplicationRevision>0</ApplicationRevision>
    <ApplicationVersion>0.4.2.0</ApplicationVersion>
    <AssemblyInformationalVersion>0.4.2.0</AssemblyInformationalVersion> 

However when I evaluate Application.ProductVersion now in the application code it always returns 1.0.0. I have also tried this:

var attr = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)
    as AssemblyInformationalVersionAttribute[];

MessageBox.Show(attr[0].InformationalVersion);

which returns the same 1.0.0 value. Building the project and checking in the Project.AssemblyInfo.cs file in the obj folder reveals:

[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

Further to this, in Visual Studio looking at the project properties page, Package, General it shows Package Version as $(VersionPrefix) with 1.0.0 underneath as the value. The Assembly version and File version fields are blank but show 1.0.0.0 underneath for their values.

My question is why the project doesn't take on the appropriate values as specified in the .csproj file? Obviously I'm missing something, but I don't believe I should need to fill in the Assembly version and File version fields manually every time I update the package.


Solution

  • Application.ProductVersion will return the value of AssemblyInformationalVersionAttribute. However, If you use Microsoft.NET.Sdk , use <InformationalVersion> instead of <AssemblyInformationalVersion> to set the AssemblyInformationalVersionAttribute. Try to change your .csproj as below:

     <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <InformationalVersion>1.2.3</InformationalVersion>
    </PropertyGroup>
    

    More detail you can refer to https://github.com/dotnet/BenchmarkDotNet/issues/382#issuecomment-284154659