views:

2923

answers:

4

I'm working on a program, and I'm trying to display the assembly FILE version

    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.FileMajorPart, fvi.FileMinorPart);
        }
    }

At the moment, this only returns the first two version numbers in the "AssemblyVersion", not "AssemblyFileVersion." I'd really like to just reference the AssemblyFileVersion rather than store an internal variable called "Version" that I have to update both this and the assembly version...

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("3.5.0")]

That's my AssemblyFileVersion from AssemblyInfo.cs. I'd like to just reference the "3.5.x" part, not the "1.0.*" :/

Thanks, Zack

+1  A: 

I guess you will have to use FileVersionInfo class.

System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)

shahkalpesh
The example I showed uses FileVersionInfo. But it only returns the "AssemblyVersion" attribute.
Zack
Oops. That is stupid of me not to see the question completely. Sorry about that.
shahkalpesh
+7  A: 

Use ProductMajorPart/ProductMinorPart instead of FileMajorPart/FileMinorPart :

    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
        }
    }
Diadistis
Err. I just realized it's getting the .dll's version! Not exactly what I am wanting. I want the executable version. (Forgot to mention that it's being called from a .dll)
Zack
Assembly asm = Assembly.GetEntryAssembly();
Diadistis
Note that fvi.FileVersion will give you the formatted string directly (all four parts), if that's what you need...
Benjol
+1  A: 

To get the version of the currently executing assembly you can use:

using System.Reflection;
Version version = Assembly.GetExecutingAssembly().GetName().Version;

The Assembly class can also load files and access all the assemblies loaded in a process.

sipwiz
+1  A: 
using System.Reflection;
using System.IO;

FileVersionInfo fv = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

Console.WriteLine("AssemblyVersion : {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());

Console.WriteLine ("AssemblyFileVersion : {0}" , fv.FileVersion.ToString ());
Vincent Duvernet