是否有任何其他属性可以在C#中检索除Environment.OsVersion之外的操作系统,因为在Mac OS的情况下此属性不确定?
答案 0 :(得分:5)
你可以做这样的事情(根据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;
}
如果您使用的是asp.net,则可以使用javascript来检测操作系统
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);