如何:检索 Windows 版本

更新:2007 年 11 月

下面的代码示例演示如何检索当前操作系统的平台和版本信息。此信息存储在 Environment.OSVersion 属性中,由粗略描述 Windows 版本的枚举和包含操作系统实际版本的 Version 对象组成。

示例

// os_ver.cpp
// compile with: /clr
using namespace System;

int main() 
{
   OperatingSystem^ osv = Environment::OSVersion;
   PlatformID id = osv->Platform;
   Console::Write("Operating system: ");

   if (id == PlatformID::Win32NT)
      Console::WriteLine("Win32NT");
   else if (id == PlatformID::Win32S)
      Console::WriteLine("Win32S");
   else if (id == PlatformID::Win32Windows)
      Console::WriteLine("Win32Windows");
   else
      Console::WriteLine("WinCE");

   Version^ version = osv->Version;
   if (version)
   {
      int build = version->Build;
      int major = version->Major;
      int minor = version->Minor;
      int revision = Environment::Version->Revision;
      Console::Write("OS Version: ");
      Console::WriteLine("{0}.{1}.{2}.{3}", 
                   build, major, minor, revision);
   }

   return 0;
}

请参见

其他资源

Windows 操作 (C++)

.NET 编程指南