sarsnake
sarsnake

Reputation: 27723

Version number in .NET Compact Framework application

I need to display the .NET Compact Framework version number on the screen. I am using .NET CF 2.0 with Windows CE 4.0.

So far, I have been ignoring the version number completely. Do I need to add anything to the assembly? how do I retrieve it programmatically?


Unfortunately this does not apply to Compact Framework. The Application.ProductVersion property doesn't exist in Compact Framework. The latest part of your answer applies though.

One more question: do these properties (major, minor, build, revision) get incremented automatically or do I set them whenever I want to? The way I see it, the revision should be automatically incremented with each new build.

Upvotes: 5

Views: 8979

Answers (4)

Stéphane B.
Stéphane B.

Reputation: 3280

To display the product version with .NET Compact Framework ( tested with with 2.0 and 3.5 ), you can use AssemblyHelper.getProductVersion() defined below.

For example, if the assembly version is defined like below in AssemblyInfo.cs file, the result of the method is "1.2.3".

Extract of AssemblyInfo.cs file :

[assembly: AssemblyVersion("1.2.3")]

Extract of AssemblyHelper.cs file :

using System;
using System.Reflection;

public static class AssemblyHelper
{
  public static string getProductVersion()
  {
    Version assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
    return String.Format("{0}.{1}.{2}", assemblyVersion.Major, assemblyVersion.Minor, assemblyVersion.Build);
  }
}

Upvotes: 0

jarmst
jarmst

Reputation: 39

I know this is an old question, but here is a solution I found using Reflection and Linq (reposted from my answer here).

First, I added this to the AssemblyInfo.cs (replace the string with whatever you want to use):

[assembly: AssemblyInformationalVersion("1.0.0.0 Alpha")]

Then, you can use this method to pull out the attribute (I placed it inside a static class in the AssemblyInfo.cs file). The method get's an array of all Assembly attributes, then selects the first result matching the attribute name (and casts it to the proper type). The InformationalVersion string can then be accessed.

//using System.Reflection;
//using System.Linq;
public static string AssemblyInformationalVersion
{
    get
    {
        AssemblyInformationalVersionAttribute informationalVersion = (AssemblyInformationalVersionAttribute) 
            (AssemblyInformationalVersionAttribute.GetCustomAttributes(Assembly.GetExecutingAssembly())).Where( 
                at => at.GetType().Name == "AssemblyInformationalVersionAttribute").First();

        return informationalVersion.InformationalVersion;
    }
}

To get the normal "AssemblyVersion" attribute I used:

//using System.Reflection;
public static string AssemblyVersion
{
    get
    {
        return Assembly.GetExecutingAssembly().GetName().Version.ToString();
    }
}

Upvotes: 1

Malik Amurlayev
Malik Amurlayev

Reputation: 2608

Upvotes: 6

VBNight
VBNight

Reputation: 1744

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Major System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Minor System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision

Source: http://msdn.microsoft.com/en-us/library/system.version.aspx

(Edit)

Application.ProductVersion Property

Gets the product version associated with this application.

Not Available In Compact Framework But System.Reflection.Assembly.GetExecutingAssembly().GetName().Version Is.

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.productversion.aspx

Upvotes: 12

Related Questions