C# 获取windows版本

以下是核心代码
public enum WindowsEnmu
{
    Windows95,
    Windows98_V2,
    Windows98,
    WindowsMe,
    WindowsNT_351,
    WindowsNT_40,
    Windows2000,
    Windows_XP,
    Windows_2003,
    WindowsVista,
    Windows7,
    Windows10
}

public WindowsEnmu GetWindowsVersion()
{
    WindowsEnmu WinVersion = WindowsEnmu.Windows_XP;
    OperatingSystem os = Environment.OSVersion;
    switch (os.Platform)
    {
        case PlatformID.Win32Windows:
            switch (os.Version.Minor)
            {
                case 0:
                    WinVersion = WindowsEnmu.Windows95;
                    break;
                case 10:
                    if (os.Version.Revision.ToString() == "2222A ")
                        WinVersion = WindowsEnmu.Windows98_V2;
                    else
                        WinVersion = WindowsEnmu.Windows98;
                    break;
                case 90:
                    WinVersion = WindowsEnmu.WindowsMe;
                    break;
            }
            break;
        case PlatformID.Win32NT:
            switch (os.Version.Major)
            {
                case 3:
                    WinVersion = WindowsEnmu.WindowsNT_351;
                    break;
                case 4:
                    WinVersion = WindowsEnmu.WindowsNT_40;
                    break;
                case 5:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            WinVersion = WindowsEnmu.Windows2000;
                            break;
                        case 1:
                            WinVersion = WindowsEnmu.Windows_XP;
                            break;
                        case 2:
                            WinVersion = WindowsEnmu.Windows_2003;
                            break;
                    }
                    break;
                case 6:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            WinVersion = WindowsEnmu.WindowsVista;
                            break;
                        case 1:
                            WinVersion = WindowsEnmu.Windows7;
                            break;
                        case 2:
                            WinVersion = WindowsEnmu.Windows10;
                            break;
                    }
                    break;
            }
            break;
    }
    return WinVersion;
}

评论