Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get AssemblyVersion stamped as FileVersion on the binary

I am using the Roslyn feature of generating version number from current date/time.

I can see the auto generated date/time based version number gets stamped correctly as AssemblyVersion, and I can read it at runtime using API.

Question: How do I get the same auto generated date time based version number stamped as file version, such that i can right click on the assembly in windows explorer and see the "File Version" under Details tab

I see when i explicitly tag the version number (say 1.2.3.4) it works fine, but not with the auto generated one

I am not using AssemblyInfo.cs and would like attributes set in .csproj

enter image description here

I am using dotnet cli to build using below csproj for example:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <AssemblyVersion>1.0.*</AssemblyVersion>
    <FileVersion>1.0.*</FileVersion>
    <Deterministic>false</Deterministic>
    <PackageId>Demo</PackageId>
    <Company>My Company</Company>
    <Copyright>Copyright © Xyzzy 2020</Copyright>
    <Description>Description</Description>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
    <GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute>
    <GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
    <GenerateAssemblyConfigurationAttribute>true</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>true</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute>
    <GenerateAssemblyCopyrightAttribute>true</GenerateAssemblyCopyrightAttribute>
    <GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
    <GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute>
  </PropertyGroup>
</Project>
like image 511
Rohit Sharma Avatar asked Feb 28 '20 08:02

Rohit Sharma


People also ask

What is the difference between AssemblyVersion and AssemblyFileVersion?

AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.

What is AssemblyVersion?

It's the version number used by framework during build and at runtime to locate, link, and load the assemblies. When you add reference to any assembly in your project, it's this version number that gets embedded.


2 Answers

According to Microsoft.NET.GenerateAssemblyInfo.targets project if you don't specify FileVersion property and set GenerateAssemblyFileVersionAttribute to false then FileVersion property value will be equal to AssemblyVersion property.

Try to modify you csproj file like this:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>

    <AssemblyVersion>1.0.*</AssemblyVersion>

    <!-- Comment or delete this line. -->
    <!-- <FileVersion>1.0.*</FileVersion> -->

    <Deterministic>false</Deterministic>
    <PackageId>Demo</PackageId>
    <Company>My Company</Company>
    <Copyright>Copyright © Xyzzy 2020</Copyright>
    <Description>Description</Description>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <GenerateAssemblyInfo>true</GenerateAssemblyInfo>

    <!-- Set this attribute to false. -->
    <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>

    <GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
    <GenerateAssemblyConfigurationAttribute>true</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>true</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute>
    <GenerateAssemblyCopyrightAttribute>true</GenerateAssemblyCopyrightAttribute>
    <GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
    <GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute>
  </PropertyGroup>
</Project>

This approach worked for me. After making above changes to csproj file I was able to see FileVersion in the file properties window.

like image 141
Iliar Turdushev Avatar answered Sep 28 '22 18:09

Iliar Turdushev


In the AssemblyInfo.cs file you can set File Properties (Under the Details Tab) such as File Description and File Version:

[assembly: AssemblyDescription("Scratch")]
[assembly: AssemblyFileVersion("1.2.3.4")]

enter image description here

The different AssemblyFile attributes are as follows:

FileVersionInfo.Comments = AssemblyDescription
FileVersionInfo.CompanyName = AssemblyCompany
FileVersionInfo.FileDescription = AssemblyTitle
FileVersionInfo.FileVersion = AssemblyFileVersion
FileVersionInfo.LegalCopyright = AssemblyCopyright
FileVersionInfo.LegalTrademarks = AssemblyTrademark
FileVersionInfo.ProductName = AssemblyProduct
FileVersionInfo.ProductVersion = AssemblyInformationalVersion

Note:

  • AssemblyVersion:

    Specifies the version of the assembly being attributed.

  • AssemblyFileVersion:

    Instructs a compiler to use a specific version number for the Win32 file version resource. The Win32 file version is not required to be the same as the assembly's version number.

  • AssemblyInformationalVersion:

    Defines additional version information for an assembly manifest.


Also note, the File Version number is actually at the end of the DLL file. Open the DLL in Text editor, and scroll to the end, at the end of the second last line:

cbff18129f3 NUL NUL FF SOH NUL BEL 1.0.0.0 NUL NUL SOH NUL

You can change the version in the text file but it won't work, it keeps the old version. You could probably work out what the binary lock is on the version number by doing a WinDiff / DiffMerge before and after setting the AssemblyFileVersion in the AssemblyInfo.cs

like image 20
Jeremy Thompson Avatar answered Sep 28 '22 17:09

Jeremy Thompson