Last active
March 28, 2016 21:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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