开发者

Embed application compilation time stamp

Is there an easy way to con开发者_高级运维figure in a build process to write in the time stamp of the build to be displayed in the application to have something like "This page was last updated: 06/26/2010"


One solution would be to embed this information in an assembly attribute during your build. You can do this using the MSBuild community tasks Time and AssemblyInfo tasks:

<Time>
    <Output TaskParameter="Month" PropertyName="Month" />
    <Output TaskParameter="Day" PropertyName="Day" />
    <Output TaskParameter="Year" PropertyName="Year" />
    <Output TaskParameter="Hour" PropertyName="Hour" />
    <Output TaskParameter="Minute" PropertyName="Minute" />
    <Output TaskParameter="Second" PropertyName="Second" />
</Time>

and

<AssemblyInfo CodeLanguage="CS"  
    OutputFile="$(MSBuildProjectDirectory)\GlobalInfo.cs" 
    AssemblyDescription="This page was last updated: $(Month)/$(Day)/$(Year)"
/>

You would then include the source file in your project (GlobalInfo.cs in this example). To access this value in code you would use something like this:

public static string GetAssemblyDescription(Type t)
{
    string result = String.Empty;
    var items = t.Assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
    if (items != null && items.Length > 0)
    {
        AssemblyDescriptionAttribute attrib = (AssemblyDescriptionAttribute)items[0];
        result = attrib.Description;
    }
    return result;
}

Type t = typeof(MyClass);
string description = GetAssemblyDescription(t);
Console.WriteLine(description);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜