32

We have an AssemblyInfo.cs file in our Web Application but we do have other projects in the same solution. They are mostly class libraries. I am told to display Assembly Version from AssemblyInfo.cs on our UI. I've found the following solution in SO from C# AssemblyFileVersion usage within a program

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

or

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

But this confuses me little bit. It says GetExecutingAssembly() and what if it is running on another assembly from other class libraries? Will those codes fetch from AssemblyInfo.cs file that is residing in the Web Project all the time? (which is what I want)

Community
  • 1
  • 1
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • http://stackoverflow.com/questions/2724546/how-to-get-assembly-version-not-file-version-for-another-exe This question talks about getying version of any assembly. – Srikanth Venugopalan Feb 28 '13 at 02:01
  • Thanks for but I don't need any assembly's instead I want from `AssemblyInfo.cs` and I want to make sure it is coming from there. – Tarik Feb 28 '13 at 02:02

2 Answers2

34

As the documentation states, Assembly.GetExecutingAssembly() gets the assembly that the calling code was compiled inside of.

If you want to be more explicit (and faster), you can write typeof(SomeType).Assembly, where SomeType is any type in the project you're looking for.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Then can you show me how to get Assembly Version info from `AssemblyInfo.cs`of the Web Application all the time? I just want to ensure that the field I need is coming from there. – Tarik Feb 28 '13 at 02:01
  • 1
    @Tarik: Use any type from that project. – SLaks Feb 28 '13 at 02:02
  • This answer led me to the following (VB) code: `New ApplicationServices.AssemblyInfo(Reflection.Assembly.GetExecutingAssembly())` – CrazyIvan1974 May 22 '17 at 14:19
  • Very simple. Thanks. typeof(SomeType).Assembly – Souza Oct 22 '19 at 19:42
  • @SLaks - You led me there and I gave you the upvote. However, the answer could be worded better to give it to the viewer without having to do extra work to find the answer. For me the answer was: System.Reflection.Assembly theAssembly = System.Reflection.Assembly.GetExecutingAssembly(); // and the actual Version value is: var AssemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); – Spencer Sullivan Mar 03 '21 at 17:54
-4

if you want read file then you can do this.

string path = @"d:\AssemblyInfo.cs";
        if (File.Exists(path))
        {
            // Open the file to read from.
            string[] readText = File.ReadAllLines(path);
            var versionInfoLines = readText.Where(t => t.Contains("[assembly: AssemblyVersion"));
            foreach (string item in versionInfoLines)
            {
                string version = item.Substring(item.IndexOf('(') + 2, item.LastIndexOf(')') - item.IndexOf('(') - 3);          
                Console.WriteLine(version);
            }

        }

Output

1.0.0.0
Mhd
  • 771
  • 1
  • 8
  • 15