2012-04-13 135 views
19

我无法找到真正有效的方法来正确检测我的C#progrma正在运行的平台(Windows/Linux/Mac),特别是在返回Unix且无法运行的Mac上几乎没有Linux平台的区别!如何正确检测Windows,Linux和Mac操作系统

因此,我根据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; 
     } 
    } 
+0

嗯,我会发布的答案在8小时内的时候,我会允许这样做:) – virrea 2012-04-13 09:05:07

+3

如果你有工作代码,编辑你的问题,并将其包含在问题中。 – 2012-04-13 09:05:55

+0

是的,这是我在做什么:)为什么-1? – virrea 2012-04-13 09:37:52

回答

6

也许检查出的平塔源IsRunningOnMac方法:

+1

谢谢,它似乎使用p/invoke。它会每次工作还是有机会单声道抱怨? – virrea 2012-04-13 13:16:59

相关问题