当前位置:  开发笔记 > 开发工具 > 正文

如何将MS Windows操作系统版本号转换为.NET中的产品名称?

如何解决《如何将MSWindows操作系统版本号转换为.NET中的产品名称?》经验,为你挑选了2个好方法。

如何将MS Windows操作系统版本号转换为产品名称?

例如,在.NET中,可以使用以下两个属性来确定该产品是MS Windows Vista Ultimate Edition:

Environment.OSVersion.Platform returns Win32NT

Environment.OSVersion.Version returns 6.0.6001.65536



1> Avram..:

howto net os版本

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";
}


XP x64和2003的版本号(主要和次要)相同== 5.2.这同样适用于Vista和2008 == 6.0以及2008 R2和Win7 == 6.1.如果需要区分这些系统,可以针对OSProductType.Workstation检查OSVersionInfo.OSProductType属性.(来自http://www.codeproject.com/KB/system/osversion.aspx)

2> Sean Kearon..:

您可以使用WMI获取友好的产品名称("Microsoft®WindowsServer®2008Enterprise"):

using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType()
                      select x.GetPropertyValue("Caption")).First();
return name != null ? name.ToString() : "Unknown";

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有