Nanji Mange
Nanji Mange

Reputation: 2265

Getting the Version of my C# app?

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.

Upvotes: 67

Views: 113849

Answers (7)

Rekless
Rekless

Reputation: 169

I know this is an older thread, but I wanted to show what I did in regards to this and there are some caveats to any method you use above, and this one.

    this.Text = "My Awesome App";

    //Get Version
    if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
    {
        Version ver = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
        this.Text += string.Format(" v{0}.{1}", ver.Major, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
    }
    else
    {
        var ver = Assembly.GetExecutingAssembly().GetName().Version;
        this.Text += string.Format(" v{0}.{1}", ver.Major, ver.Revision, Assembly.GetEntryAssembly().GetName().Name);
    }

Caveats are as follows;

  1. You can publish it as a click-once app or standalone and you will get the version
  2. You won't see it during testing, you will only see it after you have published it and ran it from the published location. For the longest time I was looking for it to appear during testing, but alas it only shows after publishing.
  3. Here I am only using Major and Revision. I want to see "My Awesome Appv1.3" . You can add to this if you want by using Major/Minor/Build/Revision.

Upvotes: 1

Ahmad Hamdy Hamdeen
Ahmad Hamdy Hamdeen

Reputation: 556

this.Text = this.Text + " " + Application.ProductVersion;

Upvotes: 2

Namiq Muhammedi
Namiq Muhammedi

Reputation: 9

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;

Upvotes: 1

Jay
Jay

Reputation: 106

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 + ".";

Upvotes: 8

Andrei Krasutski
Andrei Krasutski

Reputation: 5257

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

Upvotes: 2

Felix D.
Felix D.

Reputation: 5093

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

Upvotes: 129

Nitin Purohit
Nitin Purohit

Reputation: 18580

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

Upvotes: 15

Related Questions