How to: Retrieve the Windows Version (C++/CLI)

The following code example demonstrates how to retrieve the platform and version information of the current operating system. This information is stored in the Environment.OSVersion property and consists of an enumeration that describes the version of Windows in broad terms and a Version object that contains the exact build of the operating system.

Example

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

See Also

Other Resources

Windows Operations (C++/CLI)

.NET Programming in Visual C++