Solved

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

Posted on 2016-09-09
9
44 Views
Last Modified: 2016-09-09
Hi Experts

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

img001
Thanks in advance.
img_ee_001_090916.png
0
Comment
Question by:Eduardo Fuerte
  • 4
  • 4
9 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41791644
You could use the AssemblyInfo Class to get that information.
// Add a reference to Microsoft.VisualBasic.dll
// Add this using statement
using Microsoft.VisualBasic.ApplicationServices;

// Get the AssemblyInfo for the main assembly
var assemInfo = new AssemblyInfo(System.Reflection.Assembly.GetEntryAssembly());
// Access the information
Console.WriteLine (assemInfo.Version  );
Console.WriteLine ( assemInfo.CompanyName );

Open in new window

0
 

Author Comment

by:Eduardo Fuerte
ID: 41791743
Hi Fernando

I'm using C#.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41791767
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Eduardo Fuerte
ID: 41791792
Unfortunatelly since I got this error when tryed:
img003
But this code runs
img002
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

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41791821
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. Adding a reference to a project
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41791840
Perfect, Fernando!

Sorry my confusion,

Thank you very much!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41791846
Not a problem Eduardo, glad to help.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41791860
*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 -Capture.JPG
-saige-
0
 

Author Comment

by:Eduardo Fuerte
ID: 41791888
Thank you for the reply, it_saige.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question