Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Go to top

Timestamping assemblies with Build date and time.

, 27 Apr 2012
It surprised me to find that there is no simple way to find out when a .NET assembly was built. You can work it out from the revision number (provided you only use the "standard" numbering scheme) but it's not obvious. This provides a simple way to timestamp individual assemblies at build time.
GenerateTimeStampFile.zip
GenerateTimeStampFile
GenerateTimeStampFile
obj
x86
Debug
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource.read.1.tlog
GenerateResource.write.1.tlog
GenerateTimeStampFile.exe
GenerateTimeStampFile.pdb
GenerateTimeStampFile.Properties.Resources.resources
TempPE
Properties
Settings.settings
using System.IO;
using System;

namespace GenerateTimeStampFile
    {
    static class Program
        {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
            {
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
                {
                // Assume new file required
                string fileName = null;
                string nameSpace = "TimeStamp";
                string[] TimestampFile = new string[]
                    {@"using System;",
                     @"// The namespace can be overidden by the /N option:",
                     @"// GenerateTimeStampFile file.cs /N:MyNameSpace",
                     @"// Such settings will override your value here.",
                     @"namespace TimeStamp",
                     @"    {",
                     @"    /// <summary>",
                     @"    /// Static Timestamp related data.",
                     @"    /// </summary>",
                     @"    /// <remarks>",
                     @"    /// THIS FILE IS CHANGED BY EXTERNAL PROGRAMS.",
                     @"    /// Do not modify the namespace, as it may be overwritten. You can",
                     @"    ///    set the namespace with the /N option.",
                     @"    /// Do not modify the definition of BuildAt as your changes will be discarded.",
                     @"    /// Do not modify the definition of TimeStampedBy as your changes will be discarded.",
                     @"    /// </remarks>",
                     @"    public static class Timestamp",
                     @"        {",
                     @"        /// <summary>",
                     @"        /// The time stamp",
                     @"        /// </summary>",
                     @"        /// <remarks>",
                     @"        /// Do not modify the definition of BuildAt as your changes will be discarded.",
                     @"        /// </remarks>",
                     @"        public static DateTime BuildAt { get { return new DateTime(???); } } //--**",
                     @"        /// <summary>",
                     @"        /// The program that time stamped it.",
                     @"        /// </summary>",
                     @"        /// <remarks>",
                     @"        /// Do not modify the definition of TimeStampedBy as your changes will be discarded.",
                     @"        /// </remarks>",
                     @"        public static string TimeStampedBy { get { return @""???""; } } //--**",
                     @"        }",
                     @"    }" };
                for (int i = 1; i < args.Length; i++)
                    {
                    if (!args[i].StartsWith("/"))
                        {
                        // File name
                        fileName = args[i];
                        if (File.Exists(fileName))
                            {
                            TimestampFile = File.ReadAllLines(fileName);
                            }
                        }
                    else
                        {
                        // It's an option
                        if (args[i].StartsWith("/N:"))
                            {
                            // Set the namespace for the Timestamp class.
                            nameSpace = args[i].Substring(3);
                            }
                        }
                    }
                if (!string.IsNullOrWhiteSpace(fileName))
                    {
                    // We have an output location.
                    // Replace the namespace and timestamp.
                    for (int i = 0; i < TimestampFile.Length; i++)
                        {
                        string line = TimestampFile[i].Trim();
                        if (line.StartsWith("namespace"))
                            {
                            TimestampFile[i] = "namespace " + nameSpace;
                            }
                        else if (line.EndsWith("//--**"))
                            {
                            // Special
                            if (line.Contains("DateTime BuildAt"))
                                {
                                TimestampFile[i] = @"        public static DateTime BuildAt { get { return new DateTime(" +
                                                   DateTime.Now.Ticks.ToString() +
                                                   @"); } } //--**";
                                }
                            else if (line.Contains("string TimeStampedBy"))
                                {
                                TimestampFile[i] = @"        public static string TimeStampedBy { get { return @""GenerateTimeStampFile V1.0""; } } //--**";
                                }
                            }
                        }
                    File.WriteAllLines(fileName, TimestampFile);
                    }
                }
            Environment.ExitCode = 0;       //Set no error
            }
        }
    }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

OriginalGriff
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?
Follow on   Google+

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 27 Apr 2012
Article Copyright 2012 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid