FileVersionInfo.ProductVersion suddenly contains git commit hash – C#

by
Ali Hasan
.net-4.6 .net-6.0 .net-8.0 c#

Quick Fix: In the project file, disable the inclusion of the source revision in the informational version by adding the following XML:

<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>

The Problem:

In a .NET 6 WPF project, the ProductVersion obtained from FileVersionInfo unexpectedly includes a git commit hash, breaking the expected behavior of returning the version set in the project file. The change occurred after updating Visual Studio to receive updates on .NET 8, and the exact cause and solution are unclear.

The Solutions:

Solution 1: Disable inclusion of source revision in informational version

The unexpected inclusion of the git commit hash in the FileVersionInfo.ProductVersion can be resolved by adding the following line to the project file:

&lt;IncludeSourceRevisionInInformationalVersion&gt;false&lt;/IncludeSourceRevisionInInformationalVersion&gt;

This change seems to be introduced by SourceLink related changes in the SDK 8 version. For more information, refer to the GitHub issue: https://github.com/dotnet/sdk/issues/34568

Q&A

Reason behind git commit hash in ProductVersion?

SDK 8 version changes associated with SourceLink

Solution to remove git commit hash from ProductVersion

IncludeSourceRevisionInInformationalVersion can be set to false in the project file

Where to find more information?