System.BadImageFormatException: Could not load file or assembly — An attempt was made to load a program with an incorrect format
The runtime tried to load an assembly or native DLL built for a different architecture or runtime.
Exact message you may see
An attempt was made to load a program with an incorrect format
Seen on:
.NET
Meaning
Typical mix-ups are x86 vs x64 vs ARM64, a native DLL loaded by managed code of another bitness, or a .NET Framework assembly loaded where .NET Core is expected.
Common causes
- x86 process loading an x64 DLL (or the reverse)
- Wrong platform target (AnyCPU "Prefer 32-bit")
- Native dependency of the wrong architecture
- Corrupt or truncated DLL
⚡ Quick fix
- Build all projects for the same platform (x64)
- Use the matching native library (
runtimes/win-x64/native) - Check the DLL with
dumpbin /headersorcorflags
Detailed fix by platform
Visual Studio
- Project → Properties → Build → Platform target
CLI
dotnet publish -r win-x64 --self-contained false
How to diagnose
- Bitness — Process vs DLL?
- Target — AnyCPU/x86/x64?
- Native — Right runtime folder?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Most viewed in .NET
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- Unable to cast object of type System.InvalidCastException: Unable to cast object of type 'X' to type 'Y'
- NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Did this fix work for you?
Report a correction or suggest an improvement
Last updated 2 Dec 2026