开发者

Version number in Winform form text

How can I insert the assembly version number (whi开发者_C百科ch I set to auto increment) into a Winform form text?


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


Text = Application.ProductVersion

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


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.*")]


its in the System.Reflection.AssemblyName class eg.

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


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);
    }
}


System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
return fvi.ProductVersion;


To include application name and version the following one liner will do it:

Text = $"{Application.ProductName} {Application.ProductVersion}";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜