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

5

31

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 Windows Vista Ultimate Edition :

Environment.OSVersion.Platform returns Win32NT

Environment.OSVersion.Version returns 6.0.6001.65536

Tlaxcala answered 13/2, 2009 at 11:51 Comment(2)
Whole list of versions (and respective product names) is available here on MSDNSpinner
Probably you don't need to translate it. If you need a readable OS version's name like "Microsoft Windows 7 Professional" use ComputerInfo's property OSFullName. Add a reference to Microsoft.VisualBasic and using Microsoft.VisualBasic.Devices. Don't be confused by 'VisualBasic'. That works in any .Net languages. BTW ComputerInfo has some other usefull properties like physical/vrtual memory size.Nemeth
I
53

howto net os version

VB:

Public Function GetOSVersion() As String
    Select Case Environment.OSVersion.Platform
        Case PlatformID.Win32S
            Return "Win 3.1"
        Case PlatformID.Win32Windows
            Select Case Environment.OSVersion.Version.Minor
                Case 0
                    Return "Win95"
                Case 10
                    Return "Win98"
                Case 90
                    Return "WinME"
                Case Else
                    Return "Unknown"
            End Select
        Case PlatformID.Win32NT
            Select Case Environment.OSVersion.Version.Major
                Case 3
                    Return "NT 3.51"
                Case 4
                    Return "NT 4.0"
                Case 5
                    Select Case _
                        Environment.OSVersion.Version.Minor
                        Case 0
                            Return "Win2000"
                        Case 1
                            Return "WinXP"
                        Case 2
                            Return "Win2003"
                    End Select
                Case 6
                    Select Case _
                        Environment.OSVersion.Version.Minor
                        Case 0
                            Return "Vista/Win2008Server"
                        Case 1
                            Return "Win7/Win2008Server R2"
                        Case 2
                            Return "Win8/Win2012Server"
                        Case 3
                            Return "Win8.1/Win2012Server R2"
                    End Select
                Case 10  //this will only show up if the application has a manifest file allowing W10, otherwise a 6.2 version will be used
                  Return "Windows 10"
                Case Else
                    Return "Unknown"
            End Select
        Case PlatformID.WinCE
            Return "Win CE"
    End Select
End Function

C#

public string GetOSVersion()
{
  switch (Environment.OSVersion.Platform) {
    case PlatformID.Win32S:
      return "Win 3.1";
    case PlatformID.Win32Windows:
      switch (Environment.OSVersion.Version.Minor) {
        case 0:
          return "Win95";
        case 10:
          return "Win98";
        case 90:
          return "WinME";
      }
      break;

    case PlatformID.Win32NT:
      switch (Environment.OSVersion.Version.Major) {
        case 3:
          return "NT 3.51";
        case 4:
          return "NT 4.0";
        case 5:
          switch (Environment.OSVersion.Version.Minor) {
            case 0:
              return "Win2000";
            case 1:
              return "WinXP";
            case 2:
              return "Win2003";
          }
          break;

        case 6:
          switch(Environment.OSVersion.Version.Minor) {
            case 0:
              return "Vista/Win2008Server";
            case 1:
              return "Win7/Win2008Server R2";
            case 2:
              return "Win8/Win2012Server";
            case 3:
              return "Win8.1/Win2012Server R2";
          }
          break;
        case 10:  //this will only show up if the application has a manifest file allowing W10, otherwise a 6.2 version will be used
          return "Windows 10";
      }
      break;

    case PlatformID.WinCE:
      return "Win CE";
  }

  return "Unknown";
}
Interracial answered 13/2, 2009 at 12:5 Comment(5)
Why not case statements for both VB and C#? Also add an extra statement for Windows 7.Droit
The version numbers (both major and minor) for XP x64 and 2003 are identical == 5.2. The same applies to Vista and 2008 == 6.0 and to 2008 R2 and Win7 == 6.1. If you need to differentiate between these systems, you can check the OSVersionInfo.OSProductType property against OSProductType.Workstation. (From codeproject.com/KB/system/osversion.aspx)Almira
It might be worth mentioning that you wont be running any .net code on anything below xp/2003 so a lot of the above os values could be removed.Thymol
@Soenhay Windows 8 is there already. If you have a Windows 10 machine, you could run similar code to find out the numbers, then update this yourself. (That's the beauty of the edit button.)Expiration
@Expiration I had to make sure I wasn't going crazy but win8 and win10 were added after my comment: stackoverflow.com/posts/545698/revisionsSharynshashlik
C
14

You can use WMI to get the friendly product name ("Microsoft® Windows Server® 2008 Enterprise "):

using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
                      select x.GetPropertyValue("Caption")).First();
return name != null ? name.ToString() : "Unknown";
Consecution answered 6/1, 2010 at 21:50 Comment(0)
V
4

There's a C++ example at msdn http://msdn.microsoft.com/en-us/library/ms724429(VS.85).aspx, along with a note someone's added about how to wrap it up for use in [VB].net. It looks like the "missing" bit you need is the Win32 function GetProductInfo (PInvoke.net reference for this).

Between this and the answer from Avram, you should be able to assemble the full version string.

Velarize answered 13/2, 2009 at 12:14 Comment(4)
@Velarize : i based my answer on the ".net" tag that added to questionInterracial
Avram - Yeup, you did - but, P/Invoke is part of .net, and is (as far as I can tell!) the only way to add to your answer to get the "edition" part of it, i.e. "Ultimate Edition" or "Home Premium", etc..Velarize
fails for me in Win2003: Unable to find an entry point named 'GetProductInfo' in DLL 'kernel32.dll'.Graniteware
@alhambraeidos, please take the time to review the msdn documentation my answer references. Of course it won't work, as the API was introduced in Vista/Server2008 - which was a "minimum OS requirement" that was suggested in the OPs question.Velarize
C
3

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<int, string> sistemiOperativi = new Dictionary<int, string>(){
                        {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];
        }
    }
Classic answered 8/5, 2012 at 10:56 Comment(2)
Like the simplicity, and the Italian explanation XDLangmuir
Grazie tante! You could add some fallback to be future proof.Signesignet
T
1

If you just want a GUI friendly informational message I used

My.Computer.Info.OSFullName & " (" + My.Computer.Info.OSVersion + ")"

Seems to be future proof for future versions of Windows

Thermopile answered 4/3, 2011 at 17:54 Comment(2)
Please add some more information. What exactly is the reference My.Computer.Info.OSFullName? Is it .NET, a registry key or a struct?!?Gluey
msdn.microsoft.com/en-us/library/…Tamatave

© 2022 - 2024 — McMap. All rights reserved.