C#编译为单声道 - 检测操作系统

时间:2012-02-03 13:35:47

标签: c# macos mono

我正在尝试在OSX下运行一个C#应用程序,这个应用程序并不是完全无痛。要在短期内解决一些问题,我正在考虑在OSX中运行时设置一些特定的规则。

但是......我可以使用什么来确定应用程序是在Windows还是OSX下运行?

1 个答案:

答案 0 :(得分:17)

从Mono wiki(根据我的经验,OSX被确定为Unix):

int p = (int) Environment.OSVersion.Platform;
if ((p == 4) || (p == 128)) {
        Console.WriteLine ("Running on Unix");
} else {
        Console.WriteLine ("NOT running on Unix");
}

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