65.9K
CodeProject is changing. Read more.
Home

TimeStampGenerator: Command Line Tool for Inserting the Build Time Stamp into a C# File

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.86/5 (5 votes)

Apr 6, 2021

CPOL

2 min read

viewsIcon

5150

downloadIcon

84

Automatic insertion of the build time stamp into a C# file.

Background

In .NET, there is no macro or something similar to insert the current time stamp directly into the source code of an application. So I have written a small tool that does this job.

Using the Code

The command line tool "TimeStampGenerator.exe" is able to insert the current time stamp into a freely selectable C# source file. You can either create a new file or use an existing file for inserting the time stamp. If you create a new file, name it "Project.cs" and insert the following code:

namespace YourProjectName
{
  public static class Project
  {
    public static DateTime AppCreationTimeStamp = new DateTime();
  }
}

Here, a static class Project is created and in this class, there is a static member AppCreationTimeStamp for inserting the time stamp later when the build process is started. The name of the class is arbitrary but the time stamp member AppCreationTimeStamp is a fixed name. Save this file as "Project.cs" in your project folder. Alternatively, you can use an existing file. In this case, insert the following code somewhere:

DateTime AppCreationTimeStamp = new DateTime();

This code must be taken exactly as it is. Do not add or remove space characters or line breaks, but you may add statements in front of the code or comments behind it.

In order to setup the time stamp generator, perform the following steps. Here, it is assumed that the source file for inserting the time stamp has the name "Project.cs" and that this file is already added to your project:

  • Open the Windows Explorer and create the folder "Tools" in your project folder . The project folder is the folder in which the ".csproj" file is stored.
  • Copy the command line tool "TimeStampGenerator.exe" into the folder "Tools". You will find this tool in the folder "bin\Release" of the download archive.
  • In Visual Studio, click in the Solution Explorer with the right mouse button onto the project name. In the context menu, select "Properties" to open the properties page of your project.
  • Select the tab sheet for "Build Events".

  • In the text box, "Pre-build event command line", insert the following command line:
    "$(ProjectDir)\Tools\TimeStampGenerator.exe" "$(ProjectDir)\Project.cs"

If you then build your application, the file "Project.cs" is changed and the current time stamp is inserted. In the output area of Visual Studio, there should be the message:

1> TimeStampGenerator 1.0: Time stamp was successfully inserted into the file "C:\...\Project.cs".

History

Version number Date Description
1.0 2021-04-06 Initial version