C# · 12月 29, 2021

C#编译在单声道检测操作系统中

我试图得到一个运行在OSX下的C#应用​​程序,这不是完全无痛的.要在短期内解决一些问题,我正在考虑在OSX中运行时设置一些具体规则.

但是…我可以用什么来确定应用程序是在Windows或OSX下运行的?

解决方法 从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;}