Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to obtain the assembly version and other assembly data from a project programatically?

Hi Experts

Could you point how to obtain the assembly version and  other assembly data from a project programatically?

User generated image
Thanks in advance.
img_ee_001_090916.png
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Hi Fernando

I'm using C#.
Yes I know. In your project make a reference to the Microsoft.VisualBasic.dll then add a using statement using Microsoft.VisualBasic.ApplicationServices; which will allow you to use the Visual Basic methods in your C# applications. Then try running the code I posted.
Unfortunatelly since I got this error when tryed:
User generated image
But this code runs
User generated image
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
                string version = fvi.FileVersion;

Open in new window

No not a NuGet package. To add a reference to a dll open Solution Explorer right click on the References node and then in the menu that pops up click on Add Reference..., when the Reference Manager window opens type Microsoft.VisualBasic in the search box as shown below. Make sure that you click on the dll Name so that it has a check mark to the left and then click OK button. User generated image
Perfect, Fernando!

Sorry my confusion,

Thank you very much!
Not a problem Eduardo, glad to help.
*NO POINTS*

There is no big secret, these are stored as a part of the metadata for the application as attributes.  As Fernando has stated, you can use the AssemblyInformation class provided in the Microsoft.VisualBasic.dll or you can use the Assembly class in System.Reflection.

To get the information you requested we would do the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace EE_Q28968811
{
	class Program
	{
		static void Main(string[] args)
		{
			foreach (var pair in Assembly.GetEntryAssembly().EntryAssemblyMetaData())
				foreach (var value in pair.Value)
					Console.WriteLine("{0}: {1}", pair.Key, value);
			Console.ReadLine();
		}
	}

	static class Extensions
	{
		public static Dictionary<string, List<string>> EntryAssemblyMetaData(this Assembly assembly)
		{
			return (from attribute in assembly.GetCustomAttributes(false).Cast<Attribute>()
				   from property in attribute.GetType().GetProperties()
				   where !property.Name.Equals("TypeId", StringComparison.OrdinalIgnoreCase)
				   group property.GetValue(attribute, null).ToString() by attribute.GetType().Name into properties
				   select new { properties.Key, properties }).ToDictionary(x => x.Key, y => y.properties.ToList());
		}
	}
}

Open in new window

Which produces the following output -User generated image
-saige-
Thank you for the reply, it_saige.