Displaying the build date

前端 未结 25 2052
感动是毒
感动是毒 2020-11-22 11:36

I currently have an app displaying the build number in its title window. That\'s well and good except it means nothing to most of the users, who want to know if they have t

相关标签:
25条回答
  • 2020-11-22 12:28

    I needed a universal solution that worked with a NETStandard project on any platform (iOS, Android, and Windows.) To accomplish this, I decided to automatically generate a CS file via a PowerShell script. Here is the PowerShell script:

    param($outputFile="BuildDate.cs")
    
    $buildDate = Get-Date -date (Get-Date).ToUniversalTime() -Format o
    $class = 
    "using System;
    using System.Globalization;
    
    namespace MyNamespace
    {
        public static class BuildDate
        {
            public const string BuildDateString = `"$buildDate`";
            public static readonly DateTime BuildDateUtc = DateTime.Parse(BuildDateString, null, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
        }
    }"
    
    Set-Content -Path $outputFile -Value $class
    

    Save the PowerScript file as GenBuildDate.ps1 and add it your project. Finally, add the following line to your Pre-Build event:

    powershell -File $(ProjectDir)GenBuildDate.ps1 -outputFile $(ProjectDir)BuildDate.cs
    

    Make sure BuildDate.cs is included in your project. Works like a champ on any OS!

    0 讨论(0)
  • 2020-11-22 12:29

    Lots of great answers here but I feel like I can add my own because of simplicity, performance (comparing to resource-related solutions) cross platform (works with Net Core too) and avoidance of any 3rd party tool. Just add this msbuild target to the csproj.

    <Target Name="Date" BeforeTargets="CoreCompile">
        <WriteLinesToFile File="$(IntermediateOutputPath)gen.cs" Lines="static partial class Builtin { public static long CompileTime = $([System.DateTime]::UtcNow.Ticks) %3B }" Overwrite="true" />
        <ItemGroup>
            <Compile Include="$(IntermediateOutputPath)gen.cs" />
        </ItemGroup>
    </Target>
    

    and now you have Builtin.CompileTime or new DateTime(Builtin.CompileTime, DateTimeKind.Utc) if you need it that way.

    ReSharper is not gonna like it. You can ignore him or add a partial class to the project too but it works anyway.

    0 讨论(0)
  • 2020-11-22 12:31

    In 2018 some of the above solutions do not work anymore or do not work with .NET Core.

    I use the following approach which is simple and works for my .NET Core 2.0 project.

    Add the following to your .csproj inside the PropertyGroup :

        <Today>$([System.DateTime]::Now)</Today>
    

    This defines a PropertyFunction which you can access in your pre build command.

    Your pre-build looks like this

    echo $(today) > $(ProjectDir)BuildTimeStamp.txt
    

    Set the property of the BuildTimeStamp.txt to Embedded resource.

    Now you can read the time stamp like this

    public static class BuildTimeStamp
        {
            public static string GetTimestamp()
            {
                var assembly = Assembly.GetEntryAssembly(); 
    
                var stream = assembly.GetManifestResourceStream("NamespaceGoesHere.BuildTimeStamp.txt");
    
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 12:33

    Regarding the technique of pulling build date/version info from the bytes of an assembly PE header, Microsoft has changed the default build parameters beginning with Visual Studio 15.4. The new default includes deterministic compilation, which makes a valid timestamp and automatically incremented version numbers a thing of the past. The timestamp field is still present but it gets filled with a permanent value that is a hash of something or other, but not any indication of the build time.

    Some detailed background here

    For those who prioritize a useful timestamp over deterministic compilation, there is a way to override the new default. You can include a tag in the .csproj file of the assembly of interest as follows:

      <PropertyGroup>
          ...
          <Deterministic>false</Deterministic>
      </PropertyGroup>
    

    Update: I endorse the T4 text template solution described in another answer here. I used it to solve my issue cleanly without losing the benefit of deterministic compilation. One caution about it is that Visual Studio only runs the T4 compiler when the .tt file is saved, not at build time. This can be awkward if you exclude the .cs result from source control (since you expect it to be generated) and another developer checks out the code. Without resaving, they won't have the .cs file. There is a package on nuget (I think called AutoT4) that makes T4 compilation part of every build. I have not yet confronted the solution to this during production deployment, but I expect something similar to make it right.

    0 讨论(0)
  • 2020-11-22 12:36

    Add below to pre-build event command line:

    echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"
    

    Add this file as resource, now you have 'BuildDate' string in your resources.

    To create resources, see How to create and use resources in .NET.

    0 讨论(0)
  • 2020-11-22 12:36

    I used Abdurrahim's suggestion. However, it seemed to give a weird time format and also added the abbreviation for the day as part of the build date; example: Sun 12/24/2017 13:21:05.43. I only needed just the date so I had to eliminate the rest using substring.

    After adding the echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"to the pre-build event, I just did the following:

    string strBuildDate = YourNamespace.Properties.Resources.BuildDate;
    string strTrimBuildDate = strBuildDate.Substring(4).Remove(10);
    

    The good news here is that it worked.

    0 讨论(0)
提交回复
热议问题