Version number in the. Net SDK style project (core, standard,. Net5)

Time:2021-10-17

Version number in the. Net SDK style project (core, standard,. Net5)

Independent observer 24 December 2020

 

before  .NET  Framework,projectVersion numberSuch information is stored in  AssemblyInfo.cs file, viaAssemblySet properties:

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号插图

 

.NET Core  Later, the. Net project adopted a new  SDKStyle  Mode, which also includes these version information in the project file. It is no longer generated and used by default  AssemblyInfo.cs file, and if you add this file and fill in relevant information, you will be prompted that there are duplicates and the compilation fails. Although there are ways to restore previous use   Assemblyinfo.cs, but as the saying goes, do as the Romans do. Since people have changed the mode, let’s follow the specifications.

 

The graphic operation is similar to that in the past. In the attribute – packaging, there are “package version”, “assembly version” and “assembly file version”:

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号插图1

 

After editing, it will appear in the project file. The project file can be opened by right clicking on the project – edit project file (the same is true for this operation)  SDKStyle  Features of:

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号插图2

 

The specific information is generated in the propertygroup node of. Csproj:

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号插图3

 

Assembly version(AssemblyVersion)As before (wildcard * is also supported), package version(Version)Corresponds to the previous assembly information version(AssemblyInformationalVersion), assembly file version(FileVersion)Corresponding to previous(AssemblyFileVersion):

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号插图4

 

In addition, the assembly version information is bound in WPF. The method is as follows: introduce namespace and use:

.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号插图5

 

The assemblyhelper is as follows:

using System.Reflection;
/*
 *Source code managed: http://gitee.com/dlgcy/dotnetcodes
 */
namespace DotNet.Utilities
{
    /// 
    ///Assembly help class
    /// 
    public class AssemblyHelper
    {
        #Region assembly attribute accessor

        /// 
        ///Assembly title
        /// 
        public static string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }
        
        /// 
        ///Assembly version
        /// 
        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        /// 
        ///Additional version information for the assembly manifest
        /// 
        public static string AssemblyInformationalVersion
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
            }
        }

        /// 
        ///Win32 file version
        /// 
        public static string AssemblyFileVersion
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyFileVersionAttribute)attributes[0]).Version;
            }
        }

        /// 
        ///Text description of the assembly
        /// 
        public static string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// 
        ///The product name of the assembly manifest
        /// 
        public static string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// 
        ///Copyright of assembly manifest
        /// 
        public static string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// 
        ///The company name of the assembly manifest
        /// 
        public static string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        #endregion
    }
}

 

This is the same as before.

 

Finally, I wish you a happy Christmas Eve!