3

Is there any another property to retrieve the operating system in C# other than Environment.OsVersion because this property is not determining in case of Mac OS?

  • Is this an asp.net site? – theedam Apr 11 '14 at 09:48
  • [Get OS Version / Friendly Name in C#](http://stackoverflow.com/questions/6331826/get-os-version-friendly-name-in-c-sharp) ?? – huMpty duMpty Apr 11 '14 at 09:49
  • @theedam since he/she has tagged mono, I don't think so. It might be an app that runs on a MacOs using mono. – Christos Apr 11 '14 at 09:50
  • You're using Mono, aren't you? – Markus Apr 11 '14 at 09:50
  • @huMptyduMpty I don't think any of the answers could work on MacOS – oleksii Apr 11 '14 at 09:51
  • @huMptyduMpty it wasn't mono when I asked =) think Markus added that tag – theedam Apr 11 '14 at 09:52
  • In a comment you mention "display a warning message in a page in case Mac Os". Are you talking about the operating system of the server or the client? It sounds like you're wanting the operating system of the client, then both Environment.OsVersion plus Tinwor's answer are both solving the wrong problem - both get hold of the *server* operating system. – Chris Apr 11 '14 at 10:02
  • Am talking about operating system of client – user3512003 Apr 11 '14 at 10:05

1 Answers1

5

You can do something like this(according to Mono Wiki)

string msg1 = "This is a Windows operating system.";
string msg2 = "This is a Unix operating system.";
string msg3 = "This is a OSX operating system.";
string msg4 = "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;
    case PlatformID.MacOSX:
        Console.WriteLine(msg3);
     break;
    default:
        Console.WriteLine(msg4);
     break;
}

If you are using asp.net you can use javascript for detecting the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
document.write('Your OS: '+OSName);
Tinwor
  • 7,765
  • 6
  • 35
  • 56