How to get the current product version in C#?

前端 未结 9 950
粉色の甜心
粉色の甜心 2020-12-05 01:28

How can I programmatically get the current product version in C#?

My code:

VersionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName         


        
9条回答
  •  囚心锁ツ
    2020-12-05 02:07

    I had the same issue as most of you. It would always show 1.0.0.0 unless you manually went in and updated assemblyInfo.cs to the version you wanted to display. I think we wanted to display the publish version-revision number under the project properties but that doesn't seem to be an option (from what I've read).

    I'm not sure if back when these comments were made this existed, but now in the assemblyinfo.cs there is a way to do this automatically. I too was not content with having to manually update these with every publish.

    // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.*")]

    That * auto-increments with each publish. It won't be the same as the publish number you see under the project properties, but it definitely increments and is definitely better than doing it by hand.

    You then have a couple options to display it as mentioned above. I personally used this which I found on another site

    Version version = Assembly.GetExecutingAssembly().GetName().Version; lblRevision.Text = String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);

提交回复
热议问题