博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

获得操作系统版本

Posted on 2014-04-09 11:26  澍可  阅读(137)  评论(0编辑  收藏  举报
        /// <summary>
        /// Description : Get Operating System Version
        /// </summary>
        /// <param name="sOsVersionName"></param>
        /// <param name="bRaiseError"></param>
        public static void GetOsVersion(ref  string sOsVersionName, bool bRaiseError = true)
        {
            //TODO PVM This is wrong for Windows XP 64 bit, I have code somewhere for that.  Also not catering for Windows 7
            sOsVersionName = string.Empty;

            try
            {
                OperatingSystem verinfo = Environment.OSVersion;
                switch (verinfo.Platform)
                {
                    case PlatformID.Win32NT:
                        switch (verinfo.Version.Major)
                        {
                            case 3:
                                sOsVersionName = "Windows NT 3.51";
                                break;
                            case 4:
                                sOsVersionName = "Windows NT 4.0";
                                break;
                            case 5:
                                switch (verinfo.Version.Minor)
                                {
                                    case 0:
                                        sOsVersionName = "Windows 2000";
                                        break;
                                    case 1:
                                        sOsVersionName = "Windows XP";
                                        break;
                                    case 2:
                                        sOsVersionName = "Windows 2003";
                                        break;
                                }
                                break;
                            case 6:
                                switch (verinfo.Version.Minor)
                                {
                                    case 0:
                                        // or Windows Server 2008, but system runs on client edition of windows generally, so just show Windows Vista
                                        sOsVersionName = "Windows Vista";
                                        break;
                                    case 1:
                                        // or Windows Server 2008 R2, but system runs on client edition of windows generally, so just show Windows 7
                                        sOsVersionName = "Windows 7";
                                        break;
                                    case 2:
                                        sOsVersionName = "Windows 8";
                                        break;
                                    case 3:
                                        sOsVersionName = "Windows 8.1";
                                        break;
                                    default:
                                        break;
                                }
                                break;
                        }
                        break;

                    case PlatformID.Win32Windows:
                        switch (verinfo.Version.Major)
                        {
                            case 4:
                                switch (verinfo.Version.Minor)
                                {
                                    case 0:
                                        sOsVersionName = "Windows 95";
                                        break;
                                    case 10:
                                        sOsVersionName = "Windows 98";
                                        break;
                                    case 90:
                                        sOsVersionName = "Windows Me";
                                        break;
                                }
                                break;
                        }
                        break;

                    case PlatformID.Win32S:
                        sOsVersionName = "Windows 32s";
                        break;
                }
            }
            catch (Exception)
            {
                if (bRaiseError)
                    throw ();
            }
        }