Getting the Version of my C# app?

72,003

Solution 1

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) !

Solution 2

System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var fieVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly .Location);
var version = fieVersionInfo.FileVersion;

Solution 3

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

Solution 4

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 + ".";
Share:
72,003
Nanji Mange
Author by

Nanji Mange

Updated on July 09, 2022

Comments

  • Nanji Mange
    Nanji Mange almost 2 years

    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.

    • Leri
      Leri about 8 years
      Depends how your application is implemented. Basically, question as it stands, can't be answered.
    • Nanji Mange
      Nanji Mange about 8 years
      @Leri It is setup created by InstallShield Express Edition 2015.
  • Nanji Mange
    Nanji Mange about 8 years
    It returns "15.2.7.0" which is my DevExpress Component version.
  • Nitin
    Nitin about 8 years
    what is executingAssembly .Location giving you? path of your application exe or something else?
  • Nanji Mange
    Nanji Mange about 8 years
    Here is all parameter. (1) Assembly Location: C:\Program Files (x86)\MyApplication\MyApplication.exe. (2) File Version/Product Version: 15.2.7.0
  • Felix D.
    Felix D. about 8 years
    Please check your AssemblyInfo.cs. What is written in there ?
  • Felix D.
    Felix D. about 8 years
    if this doesn't help anything try working with GetEntryAssembly rather then GetExecutingAssembly
  • Nanji Mange
    Nanji Mange about 8 years
    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.
  • Sinatr
    Sinatr about 8 years
    "I thought InstallShield will manage" - that's wrong assumption, see this.
  • Clonkex
    Clonkex about 2 years
    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