2012-04-13 138 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

相關問題