Add Build Time to your C# Assembly
Tags: dotnet, csharp, beginners
Published: 2021-04-27 14:44:55

---
title: Add Build Time to your C# Assembly
published: true
date: 2021-04-22 02:38:58 UTC
tags: dotnet, csharp, beginners
canonical_url: https://rmauro.dev/add-build-time-to-your-csharp-assembly/
---

 ![Add Build Time to your C# Assembly](https://res-2.cloudinary.com/dgoa5llvh/image/upload/q_auto/v1/ghost-blog-images/digital-clock-cover.jpg)

The `build time` can be useful sometimes. For instance, in a web application, we can have the version and the date that was _made the build_. Such as `version 1.1.0 build at 2021-04-21 23:11:00`.

## Prelude

Often in our developer, DevOps or infrastructure life we find ourselves wondering:

- Is this the right assembly?_ 
- Why the changes are not there? What happened?_
- Which version is this? Is this the latest version?_
- Oh man, why is this not working? Oh my gosh. I hate this... ahahhahaha_

This happens especially when we don't have control over the deployment and it's published by a third-party professional.  
  
By default, DotNet doesn't provide us that. Let's get started!  
> **_This works in .Net Core 3.1 and .NET 5._**

To make this happen we're going to tell .NET compiler while building the code to also write in the assembly `SourceRevisionId` tag the `System.DateTime.Now` or `System.DateTime.UtcNow`.

## Adding Build Time to Our Assembly

Add to the **CSPROJ** file the tag `SourceRevisionId` with the following content.

```xml
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net5.0</TargetFramework>
	</PropertyGroup>

	<PropertyGroup>
		<SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyy-MM-ddTHH:mm:ss:fffZ"))</SourceRevisionId>
	</PropertyGroup>

</Project>

```
<figcaption>App.csproj</figcaption>


This works for **Console App** , **Windows Forms** , **WPF** and **Web App**. **Xamarin** and others I have no clue about.   

Leave a comment if you know.

To get the written date using C# create the method `GetLinkerTime()` as described next.

```csharp
using System;
using System.Globalization;
using System.Reflection;

public static DateTime GetLinkerTime(Assembly assembly)
{
	const string BuildVersionMetadataPrefix = "+build";

	var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
	if (attribute?.InformationalVersion != null)
	{
		var value = attribute.InformationalVersion;
		var index = value.IndexOf(BuildVersionMetadataPrefix);
		if (index > 0)
		{
			value = value[(index + BuildVersionMetadataPrefix.Length)..];
			return DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss:fffZ", CultureInfo.InvariantCulture);
		}
	}

	return default;
}
```

<figcaption>Method GetLinkerTime</figcaption>

## Full Application - Sample Console Application

```csharp
using System;
using System.Globalization;
using System.Reflection;

namespace BuildTimeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var buildTime = GetLinkerTime(Assembly.GetEntryAssembly());
            Console.WriteLine($"Build time at {buildTime}");

            Console.WriteLine("Write any key to close");
            Console.ReadKey();
        }

        public static DateTime GetLinkerTime(Assembly assembly)
        {
            const string BuildVersionMetadataPrefix = "+build";

            var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
            if (attribute?.InformationalVersion != null)
            {
                var value = attribute.InformationalVersion;
                var index = value.IndexOf(BuildVersionMetadataPrefix);
                if (index > 0)
                {
                    value = value[(index + BuildVersionMetadataPrefix.Length)..];
                    return DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss:fffZ", CultureInfo.InvariantCulture);
                }
            }
            return default;
        }
    }
}
```

<figcaption>Sample Program.cs</figcaption>

Sample output.

![Add Build Time to your C# Assembly](https://res-2.cloudinary.com/dgoa5llvh/image/upload/q_auto/v1/ghost-blog-images/image.png)<figcaption>sample output</figcaption>

## Source Code

https://github.com/ricardodemauro/buildTimeConsoleApp-tutorial