58

I am working on desktop application. I have create a setup.

Ex. My Application. Version is 1.0.0.

I want to get the current version of my desktop application which is 1.0.0. I have tried by using Application.ProductVersion but it provides the version of my controls. (I am using DevExpress Control15.2.7, so it provides the current version as 15.2.7).

How can I get the current version of the installed application? I want to compare it to the installed version to provide a "New Version Available" functionality for my product.

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63

6 Answers6

110

The info you are looking for is in AssemblyInfo.cs.

To access the info written in there at runtime you can use the System.Reflection.Assembly.

Use System.Reflection.Assembly.GetExecutingAssembly() to get the assembly (that this line of code is in) or use System.Reflection.Assembly.GetEntryAssembly() to get the assembly your project started with (most likely this is your app).

In multi-project solutions this is something to keep in mind!

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString()
// returns 1.0.0.0

Corresponding AssemblyInfo.cs:

AssemblyInfo.cs

Corresponding EXE-properties:

EXE properties

This may be important when working with InstallShield (see comments) !

florien
  • 487
  • 5
  • 16
Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • Please check your AssemblyInfo.cs. What is written in there ? – Felix D. Apr 01 '16 at 09:09
  • 3
    if this doesn't help anything try working with `GetEntryAssembly` rather then `GetExecutingAssembly` – Felix D. Apr 01 '16 at 09:11
  • I didn't set Assembly Info. I am working 3rd party InstallShield for creating setup. I have set version and all in it but didn't set in Assembly. I thought InstallShield will manage. Anyways, I have changed Assembly info. It is working fine now. Let me know if I still understand wrong. – Nanji Mange Apr 01 '16 at 09:15
  • 3
    *"I thought InstallShield will manage"* - that's wrong assumption, see [this](http://stackoverflow.com/a/15018932/1997232). – Sinatr Apr 01 '16 at 09:18
14
System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var fieVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly .Location);
var version = fieVersionInfo.FileVersion;
Nitin
  • 18,344
  • 2
  • 36
  • 53
6

Another approach, which is basically the same as the accepted answer, is:

Version appVersion = Assembly.GetExecutingAssembly().GetName().Version;
versionLabel.Text = "v" + appVersion.Major + "." + appVersion.Minor + "." + appVersion.Build + ".";
Jay
  • 94
  • 1
  • 9
2

Get the version of a specific assembly:

private const string AssemblyName = "MyAssembly"; // Name of your assembly

public Version GetVersion()
{
    // Get all the assemblies currently loaded in the application domain.
    Assembly[] assemblies = Thread.GetDomain().GetAssemblies();

    for (int i = 0; i < assemblies.Length; i++)
    {
        if (string.Compare(assemblies[i].GetName().Name, AssemblyName) == 0)
        {
            return assemblies[i].GetName().Version;
        }
    }

    return Assembly.GetExecutingAssembly().GetName().Version; // return current version assembly or return null;
}
Andrei Krasutski
  • 4,913
  • 1
  • 29
  • 35
  • I was originally using `Assembly.GetEntryAssembly()` which worked while debugging under Visual Studio and while running standalone, but when the XAML designer loaded it would return the current Visual Studio version (or the version of some component). This solution works in all three situations :D – Clonkex May 13 '22 at 01:34
1
this.Text = this.Text + " " + Application.ProductVersion;
  • 1
    There are **5 existing answers** to this question, including a top-voted, accepted answer with over **one hundred votes**. Are you _certain_ your solution hasn't already been given? If not, why do you believe your approach improves upon the existing proposals, which have been validated by the community? Offering an explanation is _always_ useful on Stack Overflow, but it's _especially_ important where the question has been resolved to the satisfaction of both the OP and the community. Help readers out by explaining what your answer does different and when it might be preferred. – Jeremy Caney Aug 08 '23 at 00:02
0

If youe are using Windows Form applicaion, you can using just ProductVersion.

// Set form text. This is showing upper the form titel bar
this.Text = "Application name" + ProductVersion;