3

I need a very small C# library that would tell me on what operating system my C# program is running.

I don't need the OS version number, just something very general, for instance I would be happy with one of:

Windows, Mac, Linux, Android, iOS

Requirements:

  • Open Source, ideally Public Domain
  • Downloadable easily as DLL

None of the solutions at https://stackoverflow.com/questions/10138040/how-to-detect-properly-windows-linux-mac-operating-systems are robust+easy enough.

4

C# itself actually provides a method to detect what operating system you are running at a basic level. All of the OS' it can detect can be found in the link. Is this what you were looking for?

OperatingSystem os = Environment.OSVersion;
PlatformID     pid = os.Platform;
switch (pid) 
    {
    case PlatformID.Win32NT:
    case PlatformID.Win32S:
    case PlatformID.Win32Windows:
    case PlatformID.WinCE:
        Console.WriteLine("I'm on windows!");
        break;
    case PlatformID.Unix:
        Console.WriteLine("I'm a linux box!");
        break;
    case PlatformID.MacOSX:
        Console.WriteLine("I'm a mac!");
        break;
    default:
        Console.WriteLine("No Idea what I'm on!");
        break;
    }
|improve this answer|||||
  • 2
    Apparently the value is sometimes Unix even for MacOS though: stackoverflow.com/questions/10138040 (linked from my question too) – Nicolas Raoul Nov 7 '14 at 11:40
  • @Nicolas Raou: That's because OSX I Unix, too. – Stefan Steiger Jan 13 at 15:17
  • @StefanSteiger While it is technically true, such a value is not as informative as what the question asks for. If there is a PlatformID.MacOSX then a solution that gives PlatformID.MacOSX for all MacOS systems would be better. – Nicolas Raoul Jan 14 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.