.Net framework version the assembly was created in

string assemblyPath = @”<exe/ dll path>”;

var assembly = Assembly.LoadFrom(assemblyPath);

var customAttributes = assembly.GetCustomAttributesData();

var dotnetFramework = customAttributes.FirstOrDefault(attr => attr.AttributeType.Equals(typeof(TargetFrameworkAttribute)));

var frameworkVersion = string.Empty;

if (null != dotnetFramework)

{

if (dotnetFramework.ConstructorArguments.Any())

{

// first argument is the name of the framework.

frameworkVersion = (string)dotnetFramework.ConstructorArguments[0].Value;

}

}

Console.WriteLine(“Framework Version: “ + frameworkVersion);

 

This code will give you the .Net framework version the dll/exe was created in.

 

I tested it for 4.5, 4.6.1 , 4.5.2 and gives the correct info.

 

Advertisements

Verify .Net Version ‘Updates’ installed on any Machine

Sometimes we need to verify what all updates for .Net version exist on my computer. This is required because fixes of known issues are put in by Microsoft in these.

 

We have 2 ways to see these updates list:

a) Looking into the registry (Manual)

b) programmatic identify and list them

The Path to look these in Registry is  HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates

Dotnet Version Updates

Under this you will find .Net Version folders and under each such folders you will find subfolders one each for Updates name <KBXXXXXXX>…

 

You can also list these updates using a C# program:

using System;
using Microsoft.Win32;

public class GetUpdateHistory
{
public static void Main()
{
using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@”SOFTWARE\Microsoft\Updates”))
{
foreach (string baseKeyName in baseKey.GetSubKeyNames())
{
if (baseKeyName.Contains(“.NET Framework”) || baseKeyName.StartsWith(“KB”) || baseKeyName.Contains(“.NETFramework”))
{

using (RegistryKey updateKey = baseKey.OpenSubKey(baseKeyName))
{
string name = (string)updateKey.GetValue(“PackageName”, “”);
Console.WriteLine(baseKeyName + ” ” + name);
foreach (string kbKeyName in updateKey.GetSubKeyNames())
{
using (RegistryKey kbKey = updateKey.OpenSubKey(kbKeyName))
{
name = (string)kbKey.GetValue(“PackageName”, “”);
Console.WriteLine(” ” + kbKeyName + ” ” + name);

if (kbKey.SubKeyCount > 0)
{
foreach (string sbKeyName in kbKey.GetSubKeyNames())
{
using (RegistryKey sbSubKey = kbKey.OpenSubKey(sbKeyName))
{
name = (string)sbSubKey.GetValue(“PackageName”, “”);
if (name == “”)
name = (string)sbSubKey.GetValue(“Description”, “”);
Console.WriteLine(” ” + sbKeyName + ” ” + name);

}
}
}
}
}
}

}
}
}
}
}

The output of this code would be

Dotnet Version Updates names  by Code