3

I have some exe files which has been created using either .net framework 4.5 or .net core 2.1 or .net core 3.1.

I want to get framework name and version information from this DLL using only c# application.

I have written below piece of code which is beneficial and works great with DLL files but not with exe.

var dllInformation = Assembly.LoadFrom(@"D:\\MyProgram.dll");
Console.WriteLine(dllInformation.FullName);
Console.WriteLine(dllInformation.ImageRuntimeVersion);
Console.WriteLine(((TargetFrameworkAttribute)dllInformation.GetCustomAttributes(typeof(TargetFrameworkAttribute)).First()).FrameworkName);

I have also gone through these links but I didn't found them useful for exe files:

information from exe file

Determine .NET Framework version for dll

Please let me know if any suggestions available.

Manish Jain
  • 1,197
  • 1
  • 11
  • 32
  • Exe can be compiled by other compiler, different from used in .NET/.NET Core – Pavel Anikhouski May 28 '20 at 07:14
  • @mjwills I have several solutions which are available there in Azure and I want to know at once that which applications has not been migrated yet in .net core. I have been assigned to migrate all the projects which has not yet been migrated to .net core 3.1 – Manish Jain May 28 '20 at 07:17
  • @PavelAnikhouski Yes exe can be compiled by another compiler but exe can be built using .net framework. I want to know anout that only – Manish Jain May 28 '20 at 07:18

3 Answers3

4

The following program should display the version of the assembly. The program loads two assemblies during runtime using Assembly.LoadFrom method. 1) is a .NET Fx assembly and 2) is a .NET Core assembly. It loads both and displays the framework version without issues. This project is in github. If you are using the github project, you need to have .NET Core 3.1 installled.

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;

namespace net007
{
    class Program
    {
        static void Main(string[] args)
        {
            //A .net framwwork dll in the same output fodler
            //as the current executable
            var fxAssembly = Assembly.LoadFrom("fx.console.app.exe");

            //A .net core dll in the same output fodler
            //as the current executable
            var netCoreAssembly = Assembly.LoadFrom("core.console.app.dll");

            ShowFrameworkVersion(fxAssembly); //.NETFramework,Version=v4.7.2
            ShowFrameworkVersion(netCoreAssembly);//.NETCoreApp,Version = v3.1
        }

        static void ShowFrameworkVersion(Assembly assembly)
        {
            var attributes = assembly.CustomAttributes;
            foreach (var attribute in attributes)
            {
                if (attribute.AttributeType == typeof(TargetFrameworkAttribute))
                {
                    var arg = attribute.ConstructorArguments.FirstOrDefault();
                    if (arg == null)
                        throw new NullReferenceException("Unable to read framework version");
                    Console.WriteLine(arg.Value);
                }
            }
        }
    }
}
Soundararajan
  • 2,000
  • 21
  • 23
1

You can use PowerShell to detect for the target framework version:

$path = "C:\your dll here.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).CustomAttributes |
Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } | 
Select-Object -ExpandProperty ConstructorArguments | 
Select-Object -ExpandProperty value
  • I already mentioned in question that I want to know this only using C# application. – Manish Jain May 28 '20 at 08:03
  • @ManishJain Okay, so what if I embed the script into your .NET application and call it? is it solve your needs? – Phan Khuong May 28 '20 at 08:07
  • May be it depends upon the successful execution – Manish Jain May 28 '20 at 08:12
  • OP wanted a C# solution, but PowerShell is easier to use when trying to troubleshoot an already built assembly. Also, the default table output can cut off a lot of values so I prefer "Format-List" instead: `ReflectionOnlyLoadFrom($path).CustomAttributes | Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } | Format-List` – Kemuel Sanchez Feb 01 '22 at 17:35
0

This is my full class to get an "Name.exe" target framework.

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;

public class TargetVersionChecker : MarshalByRefObject
{
    public string GetTargetedFrameWork(string exePath)
    {
        Assembly fxAssembly;
        try
        {
            fxAssembly = Assembly.ReflectionOnlyLoadFrom(exePath);
            var targetFrameworkAttribute = fxAssembly.GetCustomAttributesData().FirstOrDefault(x => x.AttributeType == typeof(TargetFrameworkAttribute));
            return targetFrameworkAttribute?.ConstructorArguments.FirstOrDefault().Value.ToString();
        }
        catch (Exception ex)
        {
            // I log here the error but is to specific to our system so I removed it to be more simple code.
            return string.Empty;
        }
    }
}
Dongolo Jeno
  • 414
  • 6
  • 8