How to translate MS Windows OS version numbers into product names in .NET?

后端 未结 5 1718
长情又很酷
长情又很酷 2020-12-08 00:51

How to translate MS Windows OS version numbers into product names?

For example, in .NET the following two properties could be used to work out that the product is MS

5条回答
  •  难免孤独
    2020-12-08 01:32

    This is my solution, fastest and without select cases.

    the result may be customized as you want

     public static string SistemaOperativo
        {
            get
            {
                #region Dichiarazioni
                var osInfo = Environment.OSVersion;
                int platformID = (int)osInfo.Platform;
                int versionM = osInfo.Version.Major;
                int versionm = osInfo.Version.Minor;
                string servicePack = osInfo.ServicePack;
                #endregion
    
                #region Spiegazione logica
                /*
                 * IT: 
                 * La chiave del dizionario è il risultato del concatenamento di 
                 * PlatformID,MajorVersion,MinorVersion, tutto convertito in Int32, 
                 * per esempio Platform ID=1 MajorVersion=4 MinorVersion=0, 
                 * il risultato è 140 ossia Windows 95
                 * 
                 * EN:
                 * The key in Dictionary is the 'join' 
                 * of PlatformID,MajorVersion,MinorVersion, in int32,
                 * eg. Platform ID=1 MajorVersion=4 MinorVersion=0, 
                 * the result is '140' (Windows 95)
                */
                #endregion
                Dictionary sistemiOperativi = new Dictionary(){
                            {0, "Windows 3.1"},
                            {140, "Windows 95"},
                            {1410, "Windows 98"},
                            {1490, "Windows ME"},
                            {2351, "Windows NT 3.51"},
                            {240, "Windows 4.0"},
                            {250, "Windows 2000"},
                            {251, "Windows XP"},
                            {252, "Windows 2003"},
                            {260, "Windows Vista/Server 2008"},
                            {261, "Windows 7"},
                            {-1, "Unknown"}
                       };
                int idUnivoco = int.Parse(string.Format("{0}{1}{2}", platformID, versionM, versionm));
                string outValue = "";
                if (sistemiOperativi.TryGetValue(idUnivoco, out outValue))
                    return string.Format("{0}{1}", outValue, servicePack);
                return sistemiOperativi[-1];
            }
        }
    

提交回复
热议问题