tags:

views:

200

answers:

4

Hope fully the title was somewhat descriptive.

I have a winform application written in C# with .net 2.0. I would like to have the last compile date automatically updated to a variable for use in the about box and initial splash box. Currently I have a string variable that I update manually. Is there any way to do this?

VS2008 .net 2.0 c#

A: 

There is no inbuilt macro or similar for this. Your best bet might be a custom build task, such as on tigris, using the Time task and RegexReplace or similar. Not simple to get right. Personally I use the repository version to update AssemblyInfo - broadly related?

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
...
<SvnInfo LocalPath=".">
  <Output TaskParameter="Revision" PropertyName="BuildRev" />
</SvnInfo>
<FileUpdate Files="protobuf-net\Properties\AssemblyInfo.cs"
        Regex='(\[\s*assembly:\s*AssemblyVersion\(\s*"[^\.]+\.[^\.]+)\.([^\.]+)(\.)([^\.]+)("\)\s*\])'
        ReplacementText='$1.$2.$(BuildRev)$5' />
Marc Gravell
+3  A: 

Another trick (which you may not be able to use) is to leverage the automatic build and revision numbers generated by .NET. If your AssemblyInfo has:

[assembly: AssemblyVersion("1.0.*")]

The last two numbers are just a date/time stamp. Some code (list below) may help:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
DateTime compileDate = new DateTime((v.Build - 1) * TimeSpan.TicksPerDay + v.Revision * TimeSpan.TicksPerSecond * 2).AddYears(1999);
Richard Morgan
+1  A: 

I used this about box from codeproject.com. It was actually written by Jeff Atwood way back in 2004. It figures out the compile time by looking at the date stamp on the assembly file, or calculating it from the assembly version. Perhaps you could extract the relevant code from there.

Don Kirkby
+2  A: 

Pop this into your pre-build events;

echo public static class DateStamp { public readonly static System.DateTime BuildTime = System.DateTime.Parse("%date%"); }  > $(ProjectDir)DateStamp.cs

It creates a class called DateStamp like this;

public static class DateStamp
{ 
   public readonly static System.DateTime BuildTime = System.DateTime.Parse("14/12/2009"); 
}

And you use it like this;

Console.WriteLine("Build on " + DateStamp.BuildTime.ToShortDateString());
Steve Cooper
how do I get it to recognize "DateStamp" prior to build? I get the error ...The name 'DateStamp' does not exist in the current context
Brad
Ah, right. The file (DateStamp.cs) will be in the root folder of your project. In VS, go to the solution explorer and click the button which shows all files, including the ones not currently in your project. Find DateStamp.cs, right-click it, and choose 'include in project'. You should now find it builds as part of your project.
Steve Cooper