Winforms: C#: Show Build Date in Form.

Bryan Valencia 1 Reputation point
2021-08-23T19:35:16.32+00:00

I have a .net Winforms App
I am using a Deployment Project to build an MSI installer.

Here's what I want:
When I do a build, I want the Build Date to be stored in the APPLICATION (not USER) Settings.
I then want to display that on the main form.
I do NOT want to update this value manually.

The users HATE trying to communicate with me based on build numbers. If I ask "Are you on 2.1.23 or 2.2.4? The first question is, "Is that the one from March, or June?"
I had this whole thing set up with Echo Date... in the pre-build events, but with the latest VS2019 updates and having to reinstall the Deployment Projects package, this no longer works.

All I want is to update the Build Date at Compile Time.

Notes:
NOT using Core.
NOT using ClickOnce.

Oh, and I also don't want to capture the date that the user first runs the app, like putting a dummy value in the app settings and replacing it if the dummy value is found.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,914 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,461 Reputation points
    2021-08-24T14:49:47.347+00:00

    If using ..NET Core, add the following class to your project as per below.

    Note there is no namespace used, keep it as shown

    using System;
    using System.Globalization;
    
    [AttributeUsage(AttributeTargets.Assembly)]
        internal class BuildDateAttribute : Attribute
        {
            public BuildDateAttribute(string value)
            {
                DateTime = DateTime.ParseExact(
                    value, 
                    "yyyyMMddHHmmss", 
                    CultureInfo.InvariantCulture, 
                    DateTimeStyles.None);
            }
    
            public DateTime DateTime { get; }
        }
    

    Add this to the .csproj file

    <ItemGroup>
        <AssemblyAttribute Include="BuildDateAttribute">
            <_Parameter1>$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</_Parameter1>
        </AssemblyAttribute>
    </ItemGroup>
    

    Add the following to the class which gets the build date

    private static DateTime GetBuildDate(Assembly assembly)
    {
        var attribute = assembly.GetCustomAttribute<BuildDateAttribute>();
        return attribute?.DateTime ?? default(DateTime);
    }
    

    Get the build date-time

    DateTime buildDate = GetBuildDate(Assembly.GetExecutingAssembly());
    

    Original source

    3 people found this answer helpful.

  2. Sam of Simple Samples 5,546 Reputation points
    2021-08-23T22:03:06.197+00:00

    I tried the answer in c# - Displaying the build date - Stack Overflow and got it to work but it is a little vague. It begins by saying to add the following as a pre-build event command line:

    echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"
    

    Then it is vague about what to do next. For me, I had to add the Resources folder to my project. Then build the project so the file is created. Then as the other answer indicates, in Solution Explorer right-click the project and select "Properties". Then go to the "Resources" tab. Then click the drop-down next to Add Resource. Select Add Existing File.... Browse to the Resources\BuildDate.txt file and add it. Then in your code the contents of the file will be in Properties.Resources.BuildDate. You can fine-tune that based on requirements and preferences.

    1 person found this answer helpful.

  3. Castorix31 86,406 Reputation points
    2021-08-23T20:34:27.343+00:00

    Maybe you can get the last date when the app has been built or rebuilt by reading when the .exe has been written for the last time :

      var dt = System.IO.File.GetLastWriteTime(Assembly.GetExecutingAssembly().Location); 
    

  4. Jack J Jun 24,616 Reputation points Microsoft Vendor
    2021-08-24T02:38:24.687+00:00

    @Bryan Valencia , based on my test, I find SimpleSamples' answer is correct.

    You could try the following steps to add the BuildDate.txt file and show the Build Date.

    First, Please right-click your winform and choose Properties.

    Second, Click Resource and Choose Add Resouces ->Add New Text File Called BuildDate.txt.

    125797-image.png

    Third, please write the code in the Pre-Build Event command line.

    echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"  
    

    Finally, we show the Build Date in Form.

     private void Form1_Load(object sender, EventArgs e)  
            {  
                string strCompTime = Properties.Resources.BuildDate;  
                textBox1.Text = strCompTime;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  5. Castorix31 86,406 Reputation points
    2021-08-24T06:23:20.693+00:00

    Or a better method is : Getting the date of build of a .NET assembly at runtime

    You must set :

     <Deterministic>false</Deterministic>
    

    in the .csproj file to make it working (in the right PropertyGroup)

    (tested on Windows 10 1909 with VS 2019)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.