Displaying the build date

前端 未结 25 2444
感动是毒
感动是毒 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: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 :

        $([System.DateTime]::Now)
    

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

提交回复
热议问题