The actual cause is much simpler than other answers described.
When you compile a WPF on .NET 6 project, you have several compilation options,
-
dotnet build -p:Platform=x86
-
dotnet build -p:Platform=x64
-
dotnet build -p:Platform=AnyCPU
(usually the default of VS)
I ignored ARM64 but it's similar.
You should notice that the output folder contains both yourproject.dll
(the actual managed assembly compiled by C# compiler) and yourproject.exe
(a native launcher executable).
So when you copy that folder to another machine, whether yourproject.exe
can work or not is fully determined by the bitness of that launcher executable added by .NET 6 SDK. Using dumpbin yourproject.exe /HEADERS
you can dump out the bitness information 8664 machine (x64)
or 14C machine (x86)
.
You can usually get
dumpbin
for a Developer Command Prompt or Developer PowerShell for Visual Studio.
Note that .NET 6 SDK adds an x64 launcher executable when you compile the project as AnyCPU (as well as x64). Thus, that executable won't run on a 32 bit machine and triggers the error dialog you saw.
The solution is also simple, that you compile the project as x86 (so .NET 6 SDK adds an x86 launcher executable). Another option is to use dotnet yourproject.dll
if .NET 6 runtime is installed on the 32 bit machine.