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

发布于 2024-07-14 03:33:42 字数 247 浏览 15 评论 0原文

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

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

Environment.OSVersion.Platform 返回 Win32NT

Environment.OSVersion.Version 返回 6.0 .6001.65536

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

九歌凝 2024-07-21 03:33:42

howto net 操作系统版本

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

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";
}
风苍溪 2024-07-21 03:33:42

您可以使用 WMI 获取友好的产品名称(“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";

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";
梦里兽 2024-07-21 03:33:42

msdn http://msdn.microsoft 有一个 C++ 示例.com/en-us/library/ms724429(VS.85).aspx,以及某人添加的关于如何将其包装以在 [VB].net 中使用的注释。 看起来您需要的“缺失”位是 Win32 函数 GetProductInfoPInvoke.net 参考)。

在此与 Avram 的答案之间,您应该能够组装完整版本字符串。

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.

美胚控场 2024-07-21 03:33:42

这是我的解决方案,最快且没有选择案例。

结果可以根据您的需要定制

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

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];
        }
    }
迟到的我 2024-07-21 03:33:42

如果您只是想要 GUI 友好的信息消息,我使用了

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

似乎是未来 Windows 版本的未来证明

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文