How can I determine if an .EXE file (not my application, not another running application, not a .NET application) is 32-bit or 64-bit in C#? The best I found is this and I'm guessing there's a native .NET call.
If you don't use third-party libraries or winAPI, here's a method that reads the header and gets the information.
enum PlatformFile : uint
{
Unknown = 0,
x86 = 1,
x64 = 2
}
static PlatformFile GetPlatformFile(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using (BinaryReader br = new BinaryReader(fs))
{
fs.Seek(0x3C, SeekOrigin.Begin);
int peOffset = br.ReadInt32();
fs.Seek(peOffset, SeekOrigin.Begin);
uint peHead = br.ReadUInt32();
if (peHead != 0x00004550)
return PlatformFile.Unknown;
fs.Seek(peOffset + 4, SeekOrigin.Begin);
ushort machine = br.ReadUInt16();
if (machine == 0x014c) // IMAGE_FILE_MACHINE_I386
return PlatformFile.x86;
else if (machine == 0x8664) // IMAGE_FILE_MACHINE_IA64 or IMAGE_FILE_MACHINE_AMD64
return PlatformFile.x64;
else
return PlatformFile.Unknown;
}
}