我找不到任何真正有效的检测C#程序正在运行的平台(Windows / Linux / Mac)的功能,特别是在Mac上返回Unix并且几乎不能与Linux平台区分开来!
所以我根据Mac的特点,做了一些不太理论,更实用的东西。
我发布工作代码作为答案。请,评论如果它也适用于你/可以改进。
谢谢 !
回应:
这是工作代码!
public enum Platform
{
Windows,
Linux,
Mac
}
public static Platform RunningPlatform()
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Unix:
// Well, there are chances MacOSX is reported as Unix instead of MacOSX.
// Instead of platform check, we'll do a feature checks (Mac specific root folders)
if (Directory.Exists("/Applications")
& Directory.Exists("/System")
& Directory.Exists("/Users")
& Directory.Exists("/Volumes"))
return Platform.Mac;
else
return Platform.Linux;
case PlatformID.MacOSX:
return Platform.Mac;
default:
return Platform.Windows;
}
}