Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiled in mono - Detect OS

Tags:

c#

macos

mono

I am trying to get a C# app running under OSX which is not exactly pain free. To work around some of the issues in the short term, I am thinking of setting up some specific rules when it is running in OSX.

But... What can I use to determine whether the app is running under Windows or OSX?

like image 241
BlueVoodoo Avatar asked Feb 03 '12 13:02

BlueVoodoo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


1 Answers

From the Mono wiki (in my experience, OSX is identified as Unix):

int p = (int) Environment.OSVersion.Platform;
if ((p == 4) || (p == 128)) {
        Console.WriteLine ("Running on Unix");
} else {
        Console.WriteLine ("NOT running on Unix");
}

Or

string msg1 = "This is a Windows operating system.";
string msg2 = "This is a Unix operating system.";
string msg3 = "ERROR: This platform identifier is invalid.";

OperatingSystem os = Environment.OSVersion;
PlatformID     pid = os.Platform;
switch (pid) 
{
    case PlatformID.Win32NT:
    case PlatformID.Win32S:
    case PlatformID.Win32Windows:
    case PlatformID.WinCE:
        Console.WriteLine(msg1);
        break;
    case PlatformID.Unix:
        Console.WriteLine(msg2);
        break;
    default:
        Console.WriteLine(msg3);
        break;
}
like image 155
ken Avatar answered Sep 22 '22 11:09

ken