11

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>
Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47
  • Are you looking for [`FileVersionInfo`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.fileversioninfo?WT.mc_id=DT-MVP-5003235)? – Reza Aghaei Mar 02 '20 at 03:37
  • Edited with pic - i want to be able to right click on the assembly, goto properties, under details tab should be able to see the same version number as assembly version – Rohit Sharma Mar 02 '20 at 04:46

2 Answers2

6

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Iliar Turdushev
  • 4,935
  • 1
  • 10
  • 23
  • Seems GenerateAssemblyFileVersionAttribute property is true by default with .csproj approach. When using AssemblyInfo.cs approach i just needed to remove AssemblyFileVersion and there was no flag to set. Thanks for your help – Rohit Sharma Mar 02 '20 at 14:15
  • @RohitSharma By reading this answer, now I can understand what you are asking. I read your question "looking for a way to read FileVersion by code". – Reza Aghaei Mar 02 '20 at 17:13
2

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

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks but i am not using AssemblyInfo.cs - that is the whole point of this question. All these settings have moved to .csproj and i want to use it instead of AssemblyInfo.cs – Rohit Sharma Mar 02 '20 at 05:05
  • @RohitSharma but .Net-Core is for building Cross-Platform DLLs, on a Linux system it has no concept of FileVersion. That's why it's not there. – Jeremy Thompson Mar 02 '20 at 05:20
  • Err.. i see FileVersion applies to .net core and .net standard no? https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.fileversioninfo.fileversion?view=netframework-4.8 I can see if i tag 2.1.2.3 explicitly i can see file version in windows explorer so what you are saying is not true, the only time it does not work is with date/time based version number – Rohit Sharma Mar 02 '20 at 05:39
  • 1
    You are right, thanks for the link. Please put that point about works with numbers just not date/time as the 2nd line in your question. Does the date/time set it as a Major.Minor.Release.Build format? Without the wild card "auto increment" the build has not yet occurred and defeats the point. See this answer for different approaches: https://stackoverflow.com/a/15319582/495455 – Jeremy Thompson Mar 02 '20 at 05:51