tags:

views:

515

answers:

4

Is there an equivalent of __DATE__ and __TIME__ in C#?

Basically what I'm trying to do is place some build timestamping in a C# application.

One possibility I saw on Microsoft's website was to do the following:

Assembly assem = Assembly.GetExecutingAssembly();
Version vers = assem.GetName().Version;
DateTime buildDate = new DateTime(2000, 1, 1).AddDays(vers.Build).AddSeconds(vers.Revision * 2);
Console.WriteLine(vers.ToString());
Console.WriteLine(buildDate.ToString());

However, this only works if your version in AssemblyInfo.cs is "1.0..", which ours won't be.

+2  A: 

If you are trying to autogenerate your build and revision numbers, I would look into the following project on CodePlex:

http://autobuildversion.codeplex.com/

jrista
I'm not looking to generate version numbers as we already have a pre-existing convention to stick to. Just would like to embed the compile time somewhere in the app.
Soo Wei Tan
Out of curiosity, why do you need the build time embedded?
jrista
No real reason, I've often found it handy to have some kind of build time printed out as our version numbers don't bump with every build we make.
Soo Wei Tan
+1  A: 

You could look at customizing the projects .csproj file. This is just an ordinary msbuild file and you can hook into the BeforeBuild target to generate or update a file, perhaps storing this value in a custom assembly level attribute.

I'd imagine the msbuild community tasks would be useful here as they have several helpful tasks for things like token replacement within a file.

KeeperOfTheSoul
+2  A: 

I recommend customising your build script to write the date time into the file. The MSBuild Community Tasks project has a Time task that can be used for exactly this.

apathetic
Thanks, this looks like the most straightforward way to insert the timestamp somewhere.
Soo Wei Tan
A: 

Ugly, and requires certain permissions, but if this is for internal use:

            // assume the relevant using statements above...
            FileVersionInfo vi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
            FileInfo fileInfo = new System.IO.FileInfo(vi.FileName);
            DateTime createTime = fileInfo.CreationTime;
cskilbeck