简体   繁体   English

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

[英]C# compiled in mono - Detect OS

I am trying to get a C# app running under OSX which is not exactly pain free.我正在尝试让 C# 应用程序在 OSX 下运行,这并不是完全没有痛苦的。 To work around some of the issues in the short term, I am thinking of setting up some specific rules when it is running in OSX.为了在短期内解决一些问题,我正在考虑在 OSX 中运行时设置一些特定的规则。

But... What can I use to determine whether the app is running under Windows or OSX?但是...我可以使用什么来确定应用程序是在 Windows 还是 OSX 下运行?

From the Mono wiki (in my experience, OSX is identified as Unix): 从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");
}

Or 要么

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;
}

I know this is an old question but this worked for me:我知道这是一个老问题,但这对我有用:

//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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM