2

Jeff 不久前写过关于获取文件版本/日期戳的文章。除非您关闭/重新打开解决方案,否则 Visual Studio 不会增加构建,因此获取时间戳似乎是验证您正在使用的构建的最佳方式。

我将解决方案移植到 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);
    }

这似乎有效。是否有更好/更正式的方式来判断站点何时部署?

4

1 回答 1

1

我认为你最简单的方法是在你的 web.config 中有一个时间戳。

实际上有两种方法可以在 web.config 中更新它。第一种是使用自动化构建工具,例如 NAnt。它为您提供了根据需要修改 web.config 的选项。这是我使用的方法。

如果您不使用自动构建工具,您可以使用的另一个选项是在 Visual Studio 的预构建事件中添加代码,以便为您更新 web.config。这是一篇关于 Codeplex 的文章,可以帮助您入门。

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

于 2008-11-06T03:56:10.400 回答