views:

383

answers:

1

Jeff wrote about getting a file version/datestamp a while back. Visual studio doesn't increment builds unless you close/reopen the solution, so grabbing the timestamp seems to be the best way to verify what build you are using.

I ported the solution to C#

    // from http://www.codinghorror.com/blog/archives/000264.html
    protected DateTime getLinkerTimeStamp(string filepath){
        const int peHeaderOffset = 60;
        const int linkerTimestampOffset = 8;

        byte[] b = new byte[2048];
        Stream s = null;

        try {
            s = new FileStream(filepath, FileMode.Open, FileAccess.Read);
            s.Read(b, 0, 2048);
        }
        finally{
            if (s != null){
                s.Close();
            }
        }

        int i = BitConverter.ToInt32(b, peHeaderOffset);
        int secondsSince1970 = BitConverter.ToInt32(b, i + linkerTimestampOffset);
        DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
        dt = dt.AddSeconds(secondsSince1970);
        dt = dt.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dt).Hours);
        return dt;
    }

    protected DateTime getBuildTime()
    {
        System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
        return getLinkerTimeStamp(assembly.Location);
    }

Which seems to work. Is there a better / more official way to tell when a site was deployed?

+1  A: 

I think your easiest route is to have a timestamp in your web.config.

There are really two ways you can update this in your web.config. The first is to use an automated build tool such as NAnt. It gives you the option of modifying the web.config like you would want. This is the method I use.

Another option available to you if you don't use an automated build tool is to add code in your pre-build event in Visual Studio to update the web.config for you. Here is an article on Codeplex which should get you started.

http://www.codeproject.com/KB/dotnet/configmanager_net.aspx

AaronS
Thanks for the pointer!
kurious