how to show publish version in a textbox?

前端 未结 5 2085
遥遥无期
遥遥无期 2020-12-14 15:29

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

相关标签:
5条回答
  • 2020-12-14 15:59

    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
    
    0 讨论(0)
  • 2020-12-14 16:05

    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));
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-14 16:12

    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());
    
    0 讨论(0)
  • 2020-12-14 16:18

    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));
    }
    
    0 讨论(0)
提交回复
热议问题