Solved

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

Posted on 2016-09-09
9
39 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 62

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 62

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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 62

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 62

Expert Comment

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XML to SQL Table using c# 5 44
Exception in Log4Net 1 18
Performance  Html.BeginForm vs jQuery 3 10
COnsume rest client 6 8
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now