Thursday, March 31, 2011

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

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

From stackoverflow
  • 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
                        Return "Vista/Win2008Server"
                    Case Else
                        Return "Unknown"
                End Select
            Case PlatformID.WinCE
                Return "Win CE"
        End Select
    End Function
    


    C#

     public static string GetMachineOS()
     {
      if (Environment.OSVersion.Platform == PlatformID.Win32NT)
      {
       if (Environment.OSVersion.Version.Major<=4)
        return String.Format("Windows NT {0}", Environment.OSVersion.Version.ToString());
       if (Environment.OSVersion.Version.Major==5)
       {
        if (Environment.OSVersion.Version.Minor==0)
         return String.Format("Windows 2000 {0}", Environment.OSVersion.Version.ToString());
        else
         return String.Format("Windows XP {0}", Environment.OSVersion.Version.ToString());
       }
      }
    
      if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
      {
       if (Environment.OSVersion.Version.Major>=4)
       {
        if (Environment.OSVersion.Version.Minor==0)
         return String.Format("Windows 95 {0}", Environment.OSVersion.Version.ToString());    
        else if (Environment.OSVersion.Version.Minor<90)
         return String.Format("Windows 98 {0}", Environment.OSVersion.Version.ToString());
        else
         return String.Format("Windows Millenim Edition {0}", Environment.OSVersion.Version.ToString());
       }
      }
    
      return Environment.OSVersion.ToString(); //ELSE
     }
    
    Keith : Why not case statements for both VB and C#? Also add an extra statement for Windows 7.
  • 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.

    Avram : @Rob : i based my answer on the ".net" tag that added to question
    Rob : 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..
  • 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";
    

0 comments:

Post a Comment