0

I'd like to call System.DateTime.Now.ToString() when compiling and store the returned value in a const String that will not be updated by DateTime.Now.

Is there an easy way to do this? There are plenty of topics on the net about assembly, but I'm not trying to set an assembly version so it doesn't seem the right way to do so.

Cedt
  • 251
  • 1
  • 14
  • 1
    There's nothing built into the language itself, no. You'd probably need to generate source code as part of your build, at which point it really depends on which build system you're using etc. (I'd strongly encourage you to use UTC for this rather than the system local time zone, too.) – Jon Skeet Feb 21 '18 at 11:22
  • It has been done before, but not the way you expect... https://stackoverflow.com/questions/1600962/displaying-the-build-date – Marco Feb 21 '18 at 12:10

1 Answers1

0

Regarding my comment, there is of course another way to do it. By using T4. Simply add a T4 template to your project... and change it to use UTC

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".cs" #>
// <auto-generated>
//     This code was generated by a tool.
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
namespace YourStuff
{
    internal static class CompileTime
    {
        internal static string Stamp
        {
            get
            {
<#
        var stamp = System.DateTime.Now.ToString("yyyy.MM.dd HH:mm");
#>
                return "<#=stamp#>";
            }
        }
    }
}
Marco
  • 984
  • 10
  • 18
  • Thanks for your feedback ! Your solution works. I added a T4 template and tested it successfully. However, I'm having trouble using it upon compilation. Right now, I need to manually run the template to update it, and I'd like it to run automatically when compiling. I tried to set the Build Action property to "Compile" but it doesn't seem to be the right thing to do. Any idea how to get this around? – Cedt Feb 21 '18 at 13:46
  • Strange. I don't have this problem in VS2017. anyway, there is an answer here: https://stackoverflow.com/questions/1646580/get-visual-studio-to-run-a-t4-template-on-every-build – Marco Feb 21 '18 at 14:02
  • I got lazy and didn't use JoelFan's workaround because adding a .bat would have been a bit much in my opinion. I ended up downloading a VS extension to run the T4 template at each build and it's working now. Thank you for your help ! – Cedt Feb 21 '18 at 14:28
  • Interesting. There are also ways to get it done on build process. More information: https://msdn.microsoft.com/en-us/library/ee847423.aspx and https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/07/using-msbuild-properties-in-t4-templates/ – Marco Feb 22 '18 at 08:06
  • Thanks ! I had seen comments about this while reading the link you provided before, but didn't pay much attention. I'll check this out and see if there's something "light" enough to avoid using a VS extension. – Cedt Feb 22 '18 at 13:40