博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET SDK-Style 项目(Core、Standard、.NET5)中的版本号
阅读量:3965 次
发布时间:2019-05-24

本文共 4589 字,大约阅读时间需要 15 分钟。

 

之前  Framework 时,等信息是存储在 Info.cs 文件中,通过特性进行设置:

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

 

.NET  之后,.NET 项目采用了新式的 - 模式,将这些版本信息之类的也包含在项目文件里了,默认不再生成和使用 Info.cs 文件,而且如果你将这个文件添加上并填写相关信息,会提示有重复,编译不通过。虽然也有方法来恢复以前使用 AssemblyInfo.cs 的方式,但正所谓入乡随俗,既然人家改了模式,还是按规范来吧。

 

图形操作上和以前差不多,在 属性 - 打包 中有 “包版本”、“程序集版本” 和 “程序集文件版本”:

 

编辑后就会在项目文件中出现,项目文件可通过在项目上右键 - 编辑项目文件 打开(此操作也是 - 的特色):

 

具体信息就是生成在 .csproj 的 PropertyGroup 节点内:

 

程序集版本(Assembly)和以前一样(也支持通配符 *),包版本(Version)对应以前的程序集信息版本(AssemblyInformationalVersion),程序集文件版本(FileVersion)对应以前的(AssemblyFileVersion):

 

另外,这里是在 WPF 中绑定了程序集版本信息,方法如下,也就是引入命名空间和使用:

 

AssemblyHelper 如下:

using System.Reflection;/* * 源码己托管:http://gitee.com/dlgcy/dotnetcodes */namespace DotNet.Utilities{    ///     /// 程序集帮助类    ///     public class AssemblyHelper    {        #region 程序集特性访问器        ///         /// 程序集标题        ///         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);            }        }                ///         /// 程序集版本        ///         public static string AssemblyVersion        {            get            {                return Assembly.GetExecutingAssembly().GetName().Version.ToString();            }        }        ///         /// 程序集清单的其他版本信息        ///         public static string AssemblyInformationalVersion        {            get            {                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);                if (attributes.Length == 0)                {                    return "";                }                return ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;            }        }        ///         /// Win32 文件版本        ///         public static string AssemblyFileVersion        {            get            {                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);                if (attributes.Length == 0)                {                    return "";                }                return ((AssemblyFileVersionAttribute)attributes[0]).Version;            }        }        ///         /// 程序集的文本说明        ///         public static string AssemblyDescription        {            get            {                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);                if (attributes.Length == 0)                {                    return "";                }                return ((AssemblyDescriptionAttribute)attributes[0]).Description;            }        }        ///         /// 程序集清单的产品名        ///         public static string AssemblyProduct        {            get            {                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);                if (attributes.Length == 0)                {                    return "";                }                return ((AssemblyProductAttribute)attributes[0]).Product;            }        }        ///         /// 程序集清单的版权        ///         public static string AssemblyCopyright        {            get            {                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);                if (attributes.Length == 0)                {                    return "";                }                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;            }        }        ///         /// 程序集清单的公司名称        ///         public static string AssemblyCompany        {            get            {                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);                if (attributes.Length == 0)                {                    return "";                }                return ((AssemblyCompanyAttribute)attributes[0]).Company;            }        }        #endregion    }}

 

这个和以前是通用的。

 

最后祝大家平安夜快乐!

 

原创文章,转载请注明: 转载自 

本文链接地址: 

 

你可能感兴趣的文章
Windows系统进程间通信
查看>>
linux exec的用法
查看>>
C语言中如何使用宏
查看>>
Http与RPC通信协议的比较
查看>>
Source Insight的对齐问题
查看>>
ubuntu设置开机默认进入字符界面方法
查看>>
chrome 快捷键
查看>>
Linux下buffer和cache的区别
查看>>
程序员不应该再犯的五大编程错误
查看>>
[转载][转帖]Hibernate与Sleep的区别
查看>>
Linux系统的默认编码设置
查看>>
Linux系统调用
查看>>
Linux 信号signal处理机制
查看>>
Linux 信号signal处理函数
查看>>
perror简介
查看>>
linux的system () 函数详解
查看>>
在shell脚本的第一行中,必须写#!/bin/bash
查看>>
一句话##错误 'ASP 0116' 丢失脚本关闭分隔符
查看>>
文件上传漏洞之.htaccess
查看>>
常见网络安全设备默认口令
查看>>