/********* ** Private methods *********/ /// <summary>Detect whether the code is running on Mac.</summary> /// <remarks> /// This code is derived from the Mono project (see System.Windows.Forms/System.Windows.Forms/XplatUI.cs). It detects Mac by calling the /// <c>uname</c> system command and checking the response, which is always 'Darwin' for MacOS. /// </remarks> private static bool IsRunningMac() { IntPtr buffer = IntPtr.Zero; try { buffer = Marshal.AllocHGlobal(8192); if (EnvironmentUtility.uname(buffer) == 0) { string os = Marshal.PtrToStringAnsi(buffer); return(os == "Darwin"); } return(false); } catch { return(false); // default to Linux } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } }
/********* ** Public methods *********/ /// <summary>Detect the current OS.</summary> public static Platform DetectPlatform() { switch (Environment.OSVersion.Platform) { case PlatformID.MacOSX: return(Platform.Mac); case PlatformID.Unix: return(EnvironmentUtility.IsRunningMac() ? Platform.Mac : Platform.Linux); default: return(Platform.Windows); } }