how to get publish version?
Asked Answered
P

4

16

I would like to show the publish version of my desktop application. I am trying to do it with this code:

_appVersion.Content = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

The problem is that I am not getting exactly the publish version I have in my project properties. Below is a screenshot of it:

enter image description here

But I am getting 3.0.0.12546. Does someone know where is the problem?

Payson answered 27/9, 2016 at 10:8 Comment(2)
are you checking in debug mode?Lobachevsky
Nop, in release modePayson
F
12

I was also having this issue and found that the version number set in AssemblyInfo.cs was interfering with the one set in Properties:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

I usually comment those lines out of AssemblyInfo and replace them with

[assembly: AssemblyVersion("1.0.*")]

Check whether these values have been hard-coded into your AssemblyInfo file.

See this SO question for an interesting discussion on automatic versioning. When checking AssemblyInfo.cs, make sure your auto-increment (* - if you are using it) only targets AssemblyVersion and not AssemblyFileVersion.


When debugging the program, you could check the properties of the assembly in

\bin\Release\app.publish

Under the Details tab, check the version number. Does this match up with any of the settings you specified in VS?

Fabre answered 27/9, 2016 at 10:46 Comment(1)
Perfect, your solution works like a charm, thanks a lot!Payson
F
14

We can create one property which will return the Version information as mention below and we can use that property.

public string VersionLabel
{
    get
    {
        if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
        {
            Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
            return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
        }
        else
        {
            var ver = Assembly.GetExecutingAssembly().GetName().Version;
            return string.Format("Product Name: {4}, Version: {0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
        }
    }
}
Faddish answered 27/9, 2016 at 10:42 Comment(2)
Ok, so i got this to work by placing it in the Main.CS and when i called it to a string i received 1.0.7379.32346 but my version is 20.03.8.3. why am i getting the wrong version number? thanks DaveWhiteside
i just installed the software to my laptop (Deploy) and i got the correct version number to display in the Main header text. thanks for a great solution.Whiteside
F
12

I was also having this issue and found that the version number set in AssemblyInfo.cs was interfering with the one set in Properties:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

I usually comment those lines out of AssemblyInfo and replace them with

[assembly: AssemblyVersion("1.0.*")]

Check whether these values have been hard-coded into your AssemblyInfo file.

See this SO question for an interesting discussion on automatic versioning. When checking AssemblyInfo.cs, make sure your auto-increment (* - if you are using it) only targets AssemblyVersion and not AssemblyFileVersion.


When debugging the program, you could check the properties of the assembly in

\bin\Release\app.publish

Under the Details tab, check the version number. Does this match up with any of the settings you specified in VS?

Fabre answered 27/9, 2016 at 10:46 Comment(1)
Perfect, your solution works like a charm, thanks a lot!Payson
R
6
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

will get you the assembly version that exist in the AssemblyInfo.cs file, to get the publish version that you set in the publish dialog, you should use

System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion

But notice that you have to add reference to System.Deployment, and it will work only after you publish your application by right click on the project file and click publish, everytime you publish, it will increment the Revision.

If you tried to call the above line in debug mode it will not work and will throw an exception, so you can use the following code:

try
{
    return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
catch(Exception ex)
{
    return Assembly.GetExecutingAssembly().GetName().Version;
}
Rock answered 27/9, 2016 at 13:20 Comment(1)
Note that the second way works and shows the publish version when the app is published, but it will throw an error when launching the app directly from Visual Studio.Solvent
P
1

Using C# 6.0 with Lambda expression

private string GetVersion => ApplicationDeployment.IsNetworkDeployed 
    ? $"Version: {ApplicationDeployment.CurrentDeployment.CurrentVersion}" 
    : $"Version: {Application.ProductVersion}";
Plat answered 4/5, 2017 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.