Version number in .NET Compact Framework application

一笑奈何 提交于 2019-12-04 13:52:09

问题


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.


回答1:


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




回答2:


  • You can also use Version.ToString() passing the number of components to return as parameter:

    System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(3)

    This line returns Major.Minor.Build

    Source: http://msdn.microsoft.com/en-us/library/bff8h2e1(VS.80).aspx

  • There is an AssemblyInfo.cs in your project where you can edit your assembly version. To automatically increment the revision you can use something like this: 1.0.3200.*

    Source: http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute(VS.80).aspx




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/530149/version-number-in-net-compact-framework-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!