I use OperatingSystem.IsWindows() method. However, I'm not sure about the behavior when the application is running on a virtual machine. Does it use virtual machine's operating system, or PC's operating system?
It actually doesn't even check anything at runtime. It just returns true
or false
depending on the target of the .NET CLR that the application is using.
From the source code:
[NonVersionable]
public static bool IsWindows() =>
#if TARGET_WINDOWS
true;
#else
false;
#endif