Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET setting package version with version autoincrement

I am trying to create a NuGet package from my C# class library project. It uses automatic versioning set in AssemblyInfo.cs:

[assembly: AssemblyVersion("0.5.*")] 

But package information is taken from the Version property from the .csproj file.

Because of this I have to either update version twice in both files or remove version from AssemblyInfo.cs and stop using automatic versioning (looks like it is not supported in .csproj).

Is there any way to set package version (to 0.5 in my example) and keep automatic versioning of the file while updating version in only one file?

like image 784
AndrewR Avatar asked Mar 13 '26 09:03

AndrewR


1 Answers

This answer shows that it is possible to have automatic AssemblyVersion increment in a .csproj only approach. Just including below one was sufficient for me.

<Deterministic>false</Deterministic>

In order to avoid having to adjust the fixed part of the version number -- here : 0.5 -- multiple times, you can set up a variable and reference it from within the other version tags.

Below fragment of a .csproj file shows a variable _versionNumber, used in AssemblyVersion which adds the .* wildcard segment, and used as-is in Version and some more.

<Project>
  <PropertyGroup>
    <Deterministic>false</Deterministic>

    <_versionNumber>0.5</_versionNumber>
    
    <AssemblyVersion>$(_versionNumber).*</AssemblyVersion>
    <Version>$(_versionNumber)</Version>
    
    <!-- Any other version attributes of interest -->
    <FileVersion>$(_versionNumber)</FileVersion>
    <InformationalVersion>$(_versionNumber)</InformationalVersion>
    
    <!-- More tags -->
  </PropertyGroup>  
</Project>
like image 161
pfx Avatar answered Mar 15 '26 22:03

pfx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!