[C#] 獲得版本資訊

為了進行版本控制 (version control), Assembly version 是其中一個決定執行檔版本的方法. 然而除了在執行檔中檢查, 可以利用Reflection 檢查檔案版本.

而版本資訊在Project Property > Application > Assembly Information… 中進行設定.

AssemblyHelper.cs

public static class AssemblyHelper
    {
        /// <summary>
        /// Get current assembly version.
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentVersion()
        {
            string result = string.Empty;

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                Version currentVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion;
                result = currentVersion.ToString();
            }
            else {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
                result = fvi.FileVersion;
            }
            return result;
        }
    }

 

About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.