【问题标题】:C# compiled in mono - Detect OS以单声道编译的 C# - 检测操作系统
【发布时间】:2012-02-03 13:35:46
【问题描述】:

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

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

【问题讨论】:

  • 我必须确保您理解以下内容。你明白,如果你使用 Mono,你的应用程序只能在安装了 Mono 的操作系统上运行,这包括 Windows。
  • @Ramhound:真的吗?我以为 Mono 输出的 CIL 和 MS.net2.0 是一样的。如果我错了,请随时纠正我。我对 Mono 知之甚少。
  • @spender -- 在为 Mono 运行时开发时有一些注意事项。不过,大多数都有解决方法。还有一个用于迁移 .NET 项目的迁移分析器:mono-project.com/MoMA。 Mono 万岁。
  • @Ramhound:应用程序通常会在没有 Mono 的情况下在 Windows 上运行(当然,除非你碰巧使用 Mono 特定的功能——这根本不常见)。
  • @Ramhound:至少在这种情况下,Windows 不需要 Mono。我不是在 Mono 中开发,我使用的是 Visual Studio。然后我在 OSX 中使用单声道来运行运行良好的可执行文件。不过多线程有问题。下周尝试直接在VS中解决。

标签: c# macos mono


【解决方案1】:

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

【讨论】:

  • 这仍然无法区分 linux 和 OSX。
  • 实际上,较新的单声道有OSX in the platforms enum。因此,根据单声道版本,您将获得 Unix 或 MacOSX
  • @IanNorton 虽然它有枚举,但它永远不会返回 OSX。
  • @IanNorton,当PlatformIDUnix 时,您可以读取uname 的输出(即创建一个新进程) - 如果它显示Darwin,它是OS X,其他任何东西应该说Linux。 (我知道你的帖子是 4 年前的,但我认为它可能对某人有用)
【解决方案2】:

我知道这是一个老问题,但这对我有用:

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

【讨论】:

  • 简要介绍对您有用的代码(有一些可行的解决方案)。
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 2010-10-03
  • 2010-10-28
相关资源
最近更新 更多