It use the Environment.OSVersion function to get the OS info.
Reference : 如何判斷視覺化 C#.NET 中的作業系統 Service Pack 層級
And the mentioned code below:
using System; using System.Reflection; using System.Runtime.InteropServices; class Class1 { static void Main(string[] args) { Console.WriteLine(GetServicePack()); } [StructLayout(LayoutKind.Sequential)] public struct OSVERSIONINFO { public int dwOSVersionInfoSize; public int dwMajorVersion; public int dwMinorVersion; public int dwBuildNumber; public int dwPlatformId; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] public string szCSDVersion; } [DllImport("kernel32.Dll")] public static extern short GetVersionEx(ref OSVERSIONINFO o); static public string GetServicePack() { OSVERSIONINFO os = new OSVERSIONINFO(); os.dwOSVersionInfoSize=Marshal.SizeOf(typeof(OSVERSIONINFO)); GetVersionEx(ref os); if (os.szCSDVersion=="") return "No Service Pack Installed"; else return os.szCSDVersion; } }
We can get the OS version according to
//Get OS info System.OperatingSystem osInfo = System.Environment.OSVersion; //Get OS platformID System.PlatformID platformID = osInfo.Platform; //Get OS major version int versionMajor = osInfo.Version.Major; //Get OS minor version int versionMinor = osInfo.Version.Minor;
OS version info list :
OS | PlatformID | Major Version | Minor Version |
Windows95 | 1 | 4 | 0 |
Windows98 | 1 | 4 | 10 |
WindowsMe | 1 | 4 | 90 |
WindowsNT3.5 | 2 | 3 | 0 |
WindowsNT4.0 | 2 | 4 | 0 |
Windows2000 | 2 | 5 | 0 |
WindowsXP | 2 | 5 | 1 |
Windows2003 | 2 | 5 | 2 |
WindowsVista | 2 | 6 | 0 |
Windows7 | 2 | 6 | 1 |
Windows8 |
Another method :
class CheckWinVersion { public int getOSArchitecture() { string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? 32 : 64); } public string getOSInfo() { //Get Operating system information. OperatingSystem os = Environment.OSVersion; //Get version information about the os. Version vs = os.Version; //Variable to hold our return value string operatingSystem = ""; if (os.Platform == PlatformID.Win32Windows) { //This is a pre-NT version of Windows switch (vs.Minor) { case 0: operatingSystem = "95"; break; case 10: if (vs.Revision.ToString() == "2222A") operatingSystem = "98SE"; else operatingSystem = "98"; break; case 90: operatingSystem = "Me"; break; default: break; } } else if (os.Platform == PlatformID.Win32NT) { switch (vs.Major) { case 3: operatingSystem = "NT 3.51"; break; case 4: operatingSystem = "NT 4.0"; break; case 5: if (vs.Minor == 0) operatingSystem = "2000"; else operatingSystem = "XP"; break; case 6: if (vs.Minor == 0) operatingSystem = "Vista"; else operatingSystem = "7"; break; default: break; } } //Make sure we actually got something in our OS check //We don't want to just return " Service Pack 2" or " 32-bit" //That information is useless without the OS version. if (operatingSystem != "") { //Got something. Let's prepend "Windows" and get more info. operatingSystem = "Windows " + operatingSystem; //See if there's a service pack installed. if (os.ServicePack != "") { //Append it to the OS name. i.e. "Windows XP Service Pack 3" operatingSystem += " " + os.ServicePack; } //Append the OS architecture. i.e. "Windows XP Service Pack 3 32-bit" operatingSystem += " " + getOSArchitecture().ToString() + "-bit"; } //Return the information we've gathered. return operatingSystem; } } }Example:
static void Main(string[] args) { CheckWinVersion ck = new CheckWinVersion(); Console.WriteLine(ck.getOSInfo()); Console.ReadLine(); }
沒有留言:
張貼留言