61
votes

How can I insert the assembly version number (which I set to auto increment) into a Winform form text?

6

6 Answers

92
votes

Either of these will work:

var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 
this.Text = String.Format("My Application Version {0}", version);

string version = System.Windows.Forms.Application.ProductVersion; 
this.Text = String.Format("My Application Version {0}", version);

Assuming this is run on the Form you wish to display the text on

23
votes
Text = Application.ProductVersion

Quick way to get the full version as a string (e.g. "1.2.3.4")

11
votes

I'm using the following in a WinForm:

public MainForm()
{
  InitializeComponent();
  Version version = Assembly.GetExecutingAssembly().GetName().Version;
  Text = Text + " " + version.Major + "." + version.Minor + " (build " + version.Build + ")"; //change form title
}

Not showing revision number to the user, build number is enough technical info

Make sure your AssemblyInfo.cs ends in the following (remove the version it has there by default) for VisualStudio to autoincrement build and revision number. You have to update major and minor versions yourself at every release (update major version for new features, minor version when you do just fixes):

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// 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.*")]
3
votes

its in the System.Reflection.AssemblyName class eg.

Assembly.GetExecutingAssembly().GetName().Version.ToString()
2
votes

as you can see here: http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.version.aspx

class Example
{
    static void Main()
    {
        Console.WriteLine("The version of the currently executing assembly is: {0}",
            Assembly.GetExecutingAssembly().GetName().Version);

        Console.WriteLine("The version of mscorlib.dll is: {0}",
            typeof(String).Assembly.GetName().Version);
    }
}
1
votes
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
return fvi.ProductVersion;