1

I am building a .Net desktop application (C#) to be executed using mono across different platforms. But there are certain parts of the code that needs to run only in specific OSes.

I use this to determine the OS:

"Environment.OSVersion.Platform"

//Reproducible demo code.
using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Environment.OSVersion); 
        Console.WriteLine(Environment.MachineName); 
        Console.WriteLine(Environment.OSVersion.Platform);

    }
}

Platform resolves into

Windows : Win32NT

Ubuntu : Unix

Mac : Unix

The problem is the conflict with Linux and Mac. The similar command in Java "os.name" actually gives me the macOS name, while mono does not seem to differentiate between Linux and Mac, at least with these commands.

How do I figure out that the execution environment is macOS?

Macindows
  • 209
  • 3
  • 14

1 Answers1

0

In System.Runtime.InteropServices there is the RuntimeInformation class:

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
    // macOS
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165