代码之家  ›  专栏  ›  技术社区  ›  Chris Marisic

嵌入应用程序编译时间戳

  •  0
  • Chris Marisic  · 技术社区  · 16 年前

    是否有一种简单的方法可以在构建过程中配置为在应用程序中显示的构建时间戳中写入类似“此页面上次更新时间:2010年6月26日”的内容?

    1 回复  |  直到 16 年前
        1
  •  2
  •   cHao    16 年前

    一种解决方案是在构建期间将此信息嵌入到程序集属性中。可以使用msbuild community tasks time和assemblyinfo任务执行此操作:

    <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>
    

    和

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

    然后您将在项目中包含源文件(本例中为globalinfo.cs)。要在代码中访问此值,您将使用如下内容:

    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);