Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To retrieve the operating system in c#

Tags:

c#

asp.net-mvc

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?

like image 796
user3512003 Avatar asked Nov 21 '25 12:11

user3512003


1 Answers

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);
like image 62
Tinwor Avatar answered Nov 24 '25 02:11

Tinwor