At the moment I am manually updating the version field (textbox
) in my application every time I publish it. I am wondering if there is a way to have my applicat
Also we can use overloadedToString
of System.Version
using System.Deployment.Application;
public Version AssemblyVersion
{
get
{
return ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
}
YourVersionTextBox.Text = AssemblyVersion.ToString(1); // 1 - get only major
YourVersionTextBox.Text = AssemblyVersion.ToString(2); // 1.0 - get only major, minor
YourVersionTextBox.Text = AssemblyVersion.ToString(3); // 1.0.3 - get only major, minor, build
YourVersionTextBox.Text = AssemblyVersion.ToString(4); // 1.0.3.4 - get only major, minor, build, revision
Method 1 : You can use this
string version = Application.ProductVersion;
and show the version in your textBox.
Method 2 : or if you want the version parts separately, you can use this :
System.Version version2 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
now you have these :
version2.Major;
version2.Minor;
version2.Revision;
version2.Build;
and you can use them like this
string versionString= (String.Format("{0}.{1}.{2}.{3}", version2.Major, version2.Minor, version2.Revision, version2.Build));
Try this:
using System.Deployment.Application;
public Version AssemblyVersion
{
get
{
return ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
}
Then the caller to the getter property can de-reference the Major
, Minor
, Build
and Revision
properties, like this:
YourVersionTextBox.Text = AssemblyVersion.Major.ToString() + "."
+ AssemblyVersion.Minor.ToString() + "."
+ AssemblyVersion.Build.ToString() + "."
+ AssemblyVersion.Revision.ToString();
If you get an error of ApplicationDeployment
, then you can try this:
For global version accessing through Program
, declare inside Program
class:
private static Version version = new Version(Application.ProductVersion);
public static Version Version
{
get
{
return version;
}
}
Now you have Program.Version
anywhere available in the program and you can use it to get version information like this:
LabelVersion.Text = String.Format("Version {0}.{1}",
Program.Version.Major.ToString(),
Program.Version.Minor.ToString());
Don't forget to check if the application is networkdeployed otherwise it won't work in debug mode.
if (ApplicationDeployment.IsNetworkDeployed)
{
this.Text = string.Format("Your application name - v{0}",
ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4));
}