3

I have been looking high and low for a solution to this, but although the question has been asked before the answers no longer apply to .NET Standard 1.5 and its cross-platform way of thinking. Also, this question is about the OS Architecture not the .NET platform architecture.

What I Tried

The best answer, Environment.Is64BitOperatingSystem is an API that is not implemented in .NET Standard 1.5.

The answer

/// <summary>Is64s the bit operating system.</summary>
/// <returns></returns>
if (IntPtr.Size == 8)
    // 64Bit
else
    // 32bit

is not what I need. Although still possible to do in .NET Standard, it determines the bitness of the .NET platform, not the underlying OS.

Nearly all of the other replies are using [DllImport("kernel32.dll")], which I am almost certain won't work on anything but Windows.

Question

So, how do I determine the bitness of the underlying OS for all of the platforms that .NET Standard 1.5 supports (Linux, iOS, Windows, Android, etc.)?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • so far, almost all implementations i've seen in Mono used the same `IntPtr.Size` trick – Timothy Groote Aug 10 '17 at 14:44
  • Possible duplicate of [How do I tell if my application is running as a 32-bit or 64-bit application?](https://stackoverflow.com/questions/266082/how-do-i-tell-if-my-application-is-running-as-a-32-bit-or-64-bit-application) – Owen Pauling Aug 10 '17 at 14:48
  • @OwenPauling no, it's not. this is specifically about .NET standard which does not offer the kind of functionality every answer in that thread uses – Timothy Groote Aug 10 '17 at 14:49
  • How about looking at the source for the [Environment.Is64BitOperatingSystem](https://referencesource.microsoft.com/#mscorlib/system/environment.cs,1360)? – DavidG Aug 10 '17 at 14:54
  • @DavidG that code all uses windows specific code – Timothy Groote Aug 10 '17 at 14:56
  • @TimothyGroote Yes I know, that's why I commented rather than answered. It was just a suggestion for a starting point otherwise the question kinda becomes a library request. – DavidG Aug 10 '17 at 14:58
  • @TimothyGroote - As I mentioned, IntPtr.Size does not tell the bitness of the underlying OS. – NightOwl888 Aug 10 '17 at 16:07
  • 1
    Please reopen this question. It is not a duplicate of the linked question (this isn't about application bitness at all, it is about the underlying OS). – NightOwl888 Aug 10 '17 at 17:36

1 Answers1

5

You can use System.Runtime.InteropServices.RuntimeInformation.OSArchitecture

Knight King
  • 171
  • 8