2

I am currently writing an application for Mac OSX in C# using Mono. What I would like to do is to determine the version of OSX the program is running on.

I have found the constant NSAppKitVersionNumber that would fit my needs.

However, I have no idea how to access it...

I am sure it is possible therefore any help of yours would be highly appreciated!

svick
  • 236,525
  • 50
  • 385
  • 514
wojtuch
  • 188
  • 2
  • 11

2 Answers2

5

Something like this:

    [DllImport("/System/Library/Frameworks/CoreServices.framework/CoreServices")]
    internal static extern short Gestalt(int selector, ref int response);
    static string m_OSInfoString = null;
    static void InitOSInfoString()
    {
        //const int gestaltSystemVersion = 0x73797376;
        const int gestaltSystemVersionMajor = 0x73797331;
        const int gestaltSystemVersionMinor = 0x73797332;
        const int gestaltSystemVersionBugFix = 0x73797333;

        int major = 0;
        int minor = 0;
        int bugFix = 0;

        Gestalt(gestaltSystemVersionMajor, ref major);
        Gestalt(gestaltSystemVersionMinor, ref minor);
        Gestalt(gestaltSystemVersionBugFix, ref bugFix);

        if (major == 10 && minor == 5)
            RunningOnLeopard = true;
        else
        {
            RunningOnLeopard = false;
            if (major == 10 && minor == 7)
                RunningOnLion = true;
        }

        m_OSInfoString = string.Format("Mac OS X/{0}.{1}.{2}", major, minor, bugFix);
    }
TrustMe
  • 261
  • 1
  • 3
  • thanks, your solution was also applicable but I found a oneliner that also solves my problem, see my comment under the post below – wojtuch Jan 24 '13 at 10:17
0

In .NET on windows, you have the Environment.OSVersion method. You can try it to see what it gives you on Mac OS:

// Sample for the Environment.OSVersion property 
using System;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
    }
}
/*
This example produces the following results:

OSVersion: Microsoft Windows NT 5.1.2600.0
*/
ShawnW.
  • 1,771
  • 1
  • 12
  • 12
  • 1
    On my Mountain Lion `Environment.OSVersion` returns "Unix 12.2.0.0" - and with this http://stackoverflow.com/a/11697362/183422 you should be able to piece together something. – Rolf Bjarne Kvinge Jan 19 '13 at 01:17
  • Thank you very much for the hint, I solved it as following: `bool CanUseUserNotificationCenter = Environment.OSVersion.Version.Major >= 12;` – wojtuch Jan 24 '13 at 10:11