我們通過System.Environment.OSVersion.Version獲得操作系統的版本號,然後再根據版本號進行判斷操作系統是什麼版本
Operating system Version number dwMajorVersion dwMinorVersion Windows 10 10.0 10 0 Windows 8.1 6.3 6 3 Windows 8 6.2 6 2 Windows 7 6.1 6 1 Windows Server 2008 6.0 6 0 Windows Server 2003 5.2 5 2 Windows 2000 5.0 5 0
參考於:https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724834(v=vs.85).aspx
注意:在msdn官方文檔有說明 → OSVersion 屬性報告兩個相同的版本號 (6.2.0.0) Windows 8 和 Windows 8.1。在某些情況下, OSVersion 屬性可能不會返回與指定的 Windows 程序兼容性模式功能的版本相匹配的操作系統版本。
代碼:
public class GetOSystem { private const string Windows2000 = "5.0"; private const string WindowsXP = "5.1"; private const string Windows2003 = "5.2"; private const string Windows2008 = "6.0"; private const string Windows7 = "6.1"; private const string Windows8OrWindows81 = "6.2"; private const string Windows10 = "10.0"; private string OSystemName; public void setOSystemName(string oSystemName) { this.OSystemName = oSystemName; } public GetOSystem() { switch (System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor) { case Windows2000: setOSystemName("Windows2000"); break; case WindowsXP: setOSystemName("WindowsXP"); break; case Windows2003: setOSystemName("Windows2003"); break; case Windows2008: setOSystemName("Windows2008"); break; case Windows7: setOSystemName("Windows7"); break; case Windows8OrWindows81: setOSystemName("Windows8.OrWindows8.1"); break; case Windows10: setOSystemName("Windows10"); break; } Console.WriteLine(OSystemName); } }GetOSyste
class Program { static void Main(string[] args) { #region 檢測系統 new GetOSystem(); #endregion } }
輸出結果: