如何在运行时检查操作系统版本,例如在 Windows 或 Linux 上,不使用条件编译语句 [英] How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement

查看:11
本文介绍了如何在运行时检查操作系统版本,例如在 Windows 或 Linux 上,不使用条件编译语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定我的 C# 代码在哪个平台上运行?例如它是否在 Linux 或 Windows 上运行,以便我可以在运行时执行不同的代码.

How do I determine what platform my C# code is running on? for example whether it is running on Linux or windows so that I can execute different code at runtime.

我有一个 C# Windows 应用程序,我想构建它以针对 Windows 和 Linux 平台.

I have a C# Windows application that I want to build to target Windows and Linux platforms.

到目前为止,我已经创建了两个指向同一组源代码文件的项目文件.然后我使用条件编译语句,其中一个名为 LINUX 的项目.

So far I have created two project files pointing to the same set of source code files. I then use a conditional compilation statement one of the projects called LINUX.

实际代码有差异的地方我使用条件语句使用条件编译语句,例如

Where there are difference in the actual code I use conditional statements using the conditional compilation statement, for example,

#if (LINUX)
    ' Do something
#endif

有没有更好的方法来做到这一点?我真的不想有两个项目文件.

Is there a better way of doing this? I don't really want to have two project files.

推荐答案

[编者注:此答案适用于 .NET 4.7.1 之前,或适用于 .NET Core 的 Windows 兼容包之前释放.目前最好的答案是 Alex Sanséau 的堆栈溢出问题 如何在运行时检查操作系统版本,例如在 Windows 或 Linux 上,不使用条件编译语句.]

[Editor's Note: This answer was applicable before .NET 4.7.1, or before the Windows Compatibility Pack for .NET Core was released. The current best answer is Alex Sanséau's to Stack Overflow question How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement.]

您可以使用System.Environment.OSVersion.Platform检测执行平台:

You can detect the execution platform using System.Environment.OSVersion.Platform:

public static bool IsLinux
{
    get
    {
        int p = (int) Environment.OSVersion.Platform;
        return (p == 4) || (p == 6) || (p == 128);
    }
}

来自单声道常见问题:

如何检测执行平台

可以使用System.Environment.OSVersion.Platform 值检测执行平台.然而,在每种情况下,正确检测 Unix 平台都需要做更多的工作.框架的第一个版本(1.0 和 1.1)不包含任何用于 Unix 的 PlatformID 值,因此 Mono 使用了值 128.较新的框架 2.0 将 Unix 添加到 PlatformID 枚举中,但遗憾的是,不同的值:4 和更新版本的 .NET 区分了 Unix 和 macOS,为 macOS 引入了另一个值 6.

The execution platform can be detected by using the System.Environment.OSVersion.Platform value. However correctly detecting Unix platforms, in every cases, requires a little more work. The first versions of the framework (1.0 and 1.1) didn't include any PlatformID value for Unix, so Mono used the value 128. The newer framework 2.0 added Unix to the PlatformID enum but, sadly, with a different value: 4 and newer versions of .NET distinguished between Unix and macOS, introducing yet another value 6 for macOS.

这意味着为了正确检测在 Unix 平台上运行的代码,您必须检查三个值(4、6 和 128).这可确保检测代码在 Mono CLR 1.x 运行时以及 Mono 和 Microsoft CLR 2.x 运行时上执行时能够按预期工作.

This means that in order to detect properly code running on Unix platforms you must check the three values (4, 6 and 128). This ensure that the detection code will work as expected when executed on Mono CLR 1.x runtime and with both Mono and Microsoft CLR 2.x runtimes.

这篇关于如何在运行时检查操作系统版本,例如在 Windows 或 Linux 上,不使用条件编译语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆