Search code examples
c#monooperating-systemcross-platformplatform

Detecting different operating systems and platforms


I am working on a Cross-Platform open-source project. My application is going to support Android, iOS, MAC OS X, Windows and Linux.

The first idea is to use the following code:

Environment.OSVersion.Platform

But it is not enough since I cannot cover iOS and Android. I think it is better to check the OS base and version to decide on the OS type. e.g. iOS is a UNIX-based operating system. so I can check the Version or some other properties to make sure it is iOS.

Since I do not have access to Android or iOS and my search was not successful, I need your help to get these versions for each OS.

Sincerely yours, Peyman Mortazavi


Solution

  • How about this?

    public static bool IsAndroid {
       get {
          return (Type.GetType("Android.Runtime") != null);
       }
    }
    

    And perhaps:

    public static bool IsIphoneOS {
       get {
          return (Type.GetType("MonoTouch.Constants") != null);
       }
    }
    

    Those will I suppose only work on MonoTouch or Mono for Android, but you could use the same approach. Either that or rely on correct #define values for your build platform.