linux检测不到win,c# – 如何正确检测Windows,Linux和Mac操作系统
我找不到任何真正有效的檢測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;
}
}
總結
以上是生活随笔為你收集整理的linux检测不到win,c# – 如何正确检测Windows,Linux和Mac操作系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FTP服务的简介和配置详解
- 下一篇: linux prc 时区,授时时区问题解