在mono-Detect操作系统中编译的C#
本文关键字:编译 mono-Detect 操作系统 | 更新日期: 2023-09-27 18:22:26
我正在尝试让一个C#应用程序在OSX下运行,这并不是完全免费的。为了在短期内解决一些问题,我正在考虑在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;
}
我知道这是一个老问题,但这对我有效:
//using System.IO
public static string GetOS(){
if (Directory.Exists("C:''Windows")) {return "Windows";}
if (Directory.Exists("/System/Applications/Utilities/Terminal.app")) {return "macOS";}
return "Linux";
}