.net 自動生成程序集版本號


參考:http://blog.163.com/china0359@yeah/blog/static/1217194362010577545938/

上述內容中,還有版本號設置規則等內容,感覺還不錯。

 

版本號可以存放在AssemblyInfo.cs文件中,[assembly: AssemblyVersion("1.0.0.0")]可以修改為:[assembly: AssemblyVersion("1.0.0.*")];4個數字分別為主版本、次版本、內部版本號、修訂號。

前兩個是對外發布用的,要手工修改;

內部版本號,是200011日到編譯日期的天數;

內部修訂號是當天從0點0分0秒到當前時間的毫秒數。

 

該文件中除了版本號外,還有產品名稱、公司名稱等信息。

using System.Diagnostics;//取文件版本號用

           ////Application.ProductVersion;//程序集版本號
            lbVer.Text = "版本號:" + GetAssembly(typeof(System.Reflection.AssemblyVersionAttribute));

        /// <summary>
        /// 獲取程序集項目屬性內容
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetAssembly(Type type)
        {
            if (type.ToString() == "System.Reflection.AssemblyVersionAttribute")
            {//程序集版本號,要用這個方法獲取,無法用下邊的方法獲取,原因不知
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
            object[] attributes = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(type, false);
            if (attributes.Length > 0)
            {
                if (type.ToString() == "System.Reflection.AssemblyCompanyAttribute")
                {
                    #region//公司
                    System.Reflection.AssemblyCompanyAttribute company = (System.Reflection.AssemblyCompanyAttribute)attributes[0];
                    if (company.Company != "")
                    {
                        return company.Company;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyCopyrightAttribute")
                {
                    #region//版權
                    System.Reflection.AssemblyCopyrightAttribute company = (System.Reflection.AssemblyCopyrightAttribute)attributes[0];
                    if (company.Copyright != "")
                    {
                        return company.Copyright;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyTitleAttribute")
                {
                    #region//標題
                    System.Reflection.AssemblyTitleAttribute company = (System.Reflection.AssemblyTitleAttribute)attributes[0];
                    if (company.Title != "")
                    {
                        return company.Title;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyDescriptionAttribute")
                {
                    #region//備注
                    System.Reflection.AssemblyDescriptionAttribute company = (System.Reflection.AssemblyDescriptionAttribute)attributes[0];
                    if (company.Description != "")
                    {
                        return company.Description;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyProductAttribute")
                {
                    #region//產品名稱
                    System.Reflection.AssemblyProductAttribute company = (System.Reflection.AssemblyProductAttribute)attributes[0];
                    if (company.Product != "")
                    {
                        return company.Product;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyTrademarkAttribute")
                {
                    #region//商標
                    System.Reflection.AssemblyTrademarkAttribute company = (System.Reflection.AssemblyTrademarkAttribute)attributes[0];
                    if (company.Trademark != "")
                    {
                        return company.Trademark;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyConfigurationAttribute")
                {
                    #region//獲取程序集配置信息,具體什么內容,不清楚
                    System.Reflection.AssemblyConfigurationAttribute company = (System.Reflection.AssemblyConfigurationAttribute)attributes[0];
                    if (company.Configuration != "")
                    {
                        return company.Configuration;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyCultureAttribute")
                {
                    #region//獲取屬性化程序集支持的區域性,具體什么內容,不清楚
                    System.Reflection.AssemblyCultureAttribute company = (System.Reflection.AssemblyCultureAttribute)attributes[0];
                    if (company.Culture != "")
                    {
                        return company.Culture;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyVersionAttribute")
                {
                    #region//程序集版本號
                    System.Reflection.AssemblyVersionAttribute company = (System.Reflection.AssemblyVersionAttribute)attributes[0];
                    if (company.Version != "")
                    {
                        return company.Version;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyFileVersionAttribute")
                {
                    #region//文件版本號
                    System.Reflection.AssemblyFileVersionAttribute company = (System.Reflection.AssemblyFileVersionAttribute)attributes[0];
                    if (company.Version != "")
                    {
                        return company.Version;
                    }
                    #endregion
                }
                else if (type.ToString() == "System.Reflection.AssemblyInformationalVersionAttribute")
                {
                    #region//產品版本號
                    System.Reflection.AssemblyInformationalVersionAttribute company = (System.Reflection.AssemblyInformationalVersionAttribute)attributes[0];
                    if (company.InformationalVersion != "")
                    {
                        return company.InformationalVersion;
                    }
                    #endregion
                }
            }
            //如果沒有  屬性,或者  屬性為一個空字符串,則返回 .exe 的名稱 
            return System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
        }

 

 

除此以外,在項目-屬性-發布中,有個發布版本,可以設置為“隨每次發布自增自動遞增修訂號”,可以再點擊“立即發布”后修改版本號,不過CS程序好像沒什么發布的概念吧,誰對這個有方法可以說下,學習學習。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM