开发者

How to get the current product version in C#?

开发者 https://www.devze.com 2023-03-15 12:38 出处:网络
How can I programmatically get the current product version in C#? My code: VersionNumber = S开发者_Go百科ystem.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

How can I programmatically get the current product version in C#?

My code:

VersionNumber = S开发者_Go百科ystem.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

I am getting VersionNumber=1.0.0.0, but the current version is 1.0.0.12.


There are three versions: assembly, file, and product. To get the product version:

using System.Reflection;
using System.Diagnostics;
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fileVersionInfo.ProductVersion;


I got the answer to my question its Just give the reference to System.Deployment.Application and though it wont work in developement of the visual studio but it will work once the application is deployed.

//using System.Deployment.Application;
//using System.Reflection;
public string CurrentVersion
{
    get
    {
        return ApplicationDeployment.IsNetworkDeployed
               ? ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()
               : Assembly.GetExecutingAssembly().GetName().Version.ToString();
    }
} 


System.Reflection.Assembly.GetEntryAssembly().GetName().Version


Another approach to getting the product version (which is specified using the AssemblyInformationalVersionAttribute) is

private static string AssemblyProductVersion
{
    get
    {
        object[] attributes = Assembly.GetExecutingAssembly()
            .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
        return attributes.Length == 0 ?
            "" :
            ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
    }
}


In C# you need to use reflection and diagnostics

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fileVersionInfo.ProductVersion;


Try this:

var thisApp = Assembly.GetExecutingAssembly();
AssemblyName name = new AssemblyName(thisApp.FullName);
VersionNumber = "v. " + name.Version;

Also, see this Microsoft Doc on the AssemblyName.Version property.


All these answers ask for the assembly with .GetExecutingAssembly().
If you have this code in a dll, it will return the dll version number.

Swap that call for GetCallingAssembly() to get the place in your code that wanted to know.

/// <summary>
/// Returns version like 2.1.15
/// </summary>
public static String ProductVersion
{
    get
    {
        return new Version(FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location).ProductVersion).ToString();
    }
}


var productVersion = FileVersionInfo.GetVersionInfo(typeof(SomeClassFromDesiredAssembly).Assembly.Location).ProductVersion;


I had the same issue as most of you. It would always show 1.0.0.0 unless you manually went in and updated assemblyInfo.cs to the version you wanted to display. I think we wanted to display the publish version-revision number under the project properties but that doesn't seem to be an option (from what I've read).

I'm not sure if back when these comments were made this existed, but now in the assemblyinfo.cs there is a way to do this automatically. I too was not content with having to manually update these with every publish.

// You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.*")]

That * auto-increments with each publish. It won't be the same as the publish number you see under the project properties, but it definitely increments and is definitely better than doing it by hand.

You then have a couple options to display it as mentioned above. I personally used this which I found on another site

Version version = Assembly.GetExecutingAssembly().GetName().Version; lblRevision.Text = String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号