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;
    }

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.