I have a .NET exe compiled for "AnyCPU", so it can theoretically run in both 32-bit and 64-bit mode (on a 64-bit operating system). Since my exe strongly interacts with MS Office, I need to run it with the same bitness as the installed Office version. (Please take this requirement as granted, I will gladly provide details in the comments, if you are interested.)
Now, I have thought up a solution for this, but it seems more complicated than necessary:
This would work, but I'd rather have a single, simple MyExe.exe with something like the following pseudo-code during start-up:
if (Environment.Is64BitProcess && IsOffice32BitInstalled)
{
// restart myself in a 32-bit process instead of a 64-bit process
??? // <-- this is where I need help
}
Is that possible? If yes, how?
Notes: I know about the CorFlags utility, but I'd rather have a solution that does not require modifying the exe itself at runtime.
Is restarting via runtime parameter an option?
dotnet run -r win-x86
Apparently I don't have reputation enough to post comments, so posting this as an answer instead.
If, as you say, you're deploying to an internal network, I'd recommend using a launcher anwyay.
That way users can have a single network shortcut to the launcher, that doesn't move around. The launcher can determine which bitness is required, and which is the latest release, and launch the EXE for that release.
If you allow users to go directly at the EXE, you will either encounter problems with users having the EXE opened, so you can't overwrite it, or have to create new user shortcuts for every release.