Click here to Skip to main content
15,559,491 members
Articles / Programming Languages / C#
Tip/Trick
Posted 6 Apr 2021

Tagged as

Stats

3.3K views
81 downloads
4 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
3.86/5 (5 votes)
6 Apr 2021CPOL2 min read
Automatic insertion of the build time stamp into a C# file.
If you have written an application, you often need the time stamp when the application was built. You can insert the time stamp manually but this is not very reliable because you might forget it. This article presents a command line tool which automates this process.

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:

C#
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:

C#
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".

    Image 1

  • 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionC# Source Generator can be used instead of external CLI tool Pin
Hynek Syrovatka12-Apr-21 6:23
Hynek Syrovatka12-Apr-21 6:23 
Questionwhy not use a batch file Pin
bfrthekid999-Apr-21 8:13
bfrthekid999-Apr-21 8:13 
PraiseWell done! Pin
AnotherKen8-Apr-21 9:11
professionalAnotherKen8-Apr-21 9:11 
GeneralMy vote of 1 Pin
GerVenson7-Apr-21 2:52
professionalGerVenson7-Apr-21 2:52 
GeneralRe: My vote of 1 Pin
tamas mester7-Apr-21 3:54
tamas mester7-Apr-21 3:54 
GeneralRe: My vote of 1 Pin
GerVenson7-Apr-21 4:04
professionalGerVenson7-Apr-21 4:04 
GeneralRe: My vote of 1 Pin
tamas mester7-Apr-21 4:25
tamas mester7-Apr-21 4:25 
GeneralRe: My vote of 1 Pin
GerVenson7-Apr-21 5:11
professionalGerVenson7-Apr-21 5:11 
I am fully aware of your intend with that comment.

Dont reinvent the weel?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.