C # operating system version detection (record ),
We use System. Environment. OSVersion. Version to obtain the Version number of the operating System, and then determine the Version number based on the Version number.
Attribute of the Version class
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 |
Reference: https://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724834 (v = vs.85). aspx
Note: In the official msdn documentation, the description → OSVersion attribute reports two identical versions (6.2.0.0) Windows 8 and Windows 8.1. In some cases, the OSVersion attribute may not return the OS version that matches the version of the specified Windows program compatibility mode feature.
Code:
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 detection system new GetOSystem (); # endregion }}
Output result: