Skip to content

Instantly share code, notes, and snippets.

@yalayabeeb
Last active March 28, 2016 21:47
Show Gist options
  • Save yalayabeeb/47ef02d89b8f3adfef51 to your computer and use it in GitHub Desktop.
Save yalayabeeb/47ef02d89b8f3adfef51 to your computer and use it in GitHub Desktop.
A class to determine whether an assembly is managed or native. Added process architecture.
using System;
using System.IO;
using System.Reflection;
public static class FileInformation
{
public static ProcessorArchitecture GetArchitecture(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException();
}
var assembly = AssemblyName.GetAssemblyName(filePath);
return assembly.ProcessorArchitecture;
}
public static bool IsManaged(string filePath)
{
byte[] data = File.ReadAllBytes(filePath);
return IsManaged(data);
}
public static bool IsManaged(byte[] fileData)
{
return fileData[0x3C] == 0x80;
}
public static bool IsManagedUsingReflection(byte[] fileData)
{
try
{
Assembly.Load(fileData);
return true;
}
catch (Exception)
{
return false;
}
}
public static bool IsManagedUsingReflection(string filePath)
{
try
{
Assembly.Load(filePath);
return true;
}
catch (Exception)
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment