文章編號: 307394 - 上次校閱: 2006年1月18日 - 版次: 1.5

如何使用 Visual C++ 來判斷的 Windows 版本

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何判斷哪一個作業系統正在執行應用程式的系統上的使用中。它區分 Windows 95、 Windows 98、 Windows 千禧版、 Windows NT 3.51、 Windows NT 4.0、 Windows 2000 及 Windows XP。

需求

本文假設您擁有對 C + + 程式設計的中繼層級瞭解。

Windows 版本資料

若要判斷給定的系統執行的作業系統,則需下列資料:

摺疊此表格展開此表格
Windows 95Windows 98Windows MEWindows NT 4Windows 2000Windows XP
PlatformID111 222
主要版本444 455
次要版本01090 001

注意: 雖然本文的程式碼不會檢查所有 32 位元 Windows 版本,將 Visual Studio.NET 和.NET Framework 都不支援 Windows 95 或 Windows NT 3.51 上。

取得作業系統資訊

建立一個 OperatingSystem 類別的指標,並將目前的作業系統資訊指派給它:
System::OperatingSystem *osInfo = System::Environment::OSVersion;
				

判斷平台識別碼

邏輯的作業系統資訊評估之第一個步驟是判斷哪一個平台在使用中,如下所示。這是藉由使用 PlatformIDPlatformIDOperatingSystem 類別的屬性。Win32Windows 」 的列舉的值表示作業系統的 Windows 9 x 家族的產品。WinNT"指出作業系統的 Windows NT 家族。
switch(osInfo->Platform)
    {
        case System::PlatformID::Win32Windows:         
            {
             //code to determine specific Windows 9x version
            }

       case System::PlatformID::Win32NT:
           {
              //code to determine specific Windows NT version
           }
       
     }
				

決定特定 Windows 9 x 版本

如果平台已被判定為 Windows 9 x,則主要或次要版本可以加以分析,以便判斷特定版本如下列程式碼會示範。
//platform is win9x
case System::PlatformID::Win32Windows:
     
switch (osInfo->Version->Minor)
    {
        case 0:
            Console::WriteLine ("Windows 95");
            break;
        case 10:
            if(osInfo->Version->Revision.ToString()=="2222A")
                Console::WriteLine("Windows 98 Second Edition");
            else
                 Console::WriteLine("Windows 98");
                 break;
        case  90:
                 Console::WriteLine("Windows ME");
                 break;
     }break;
     

				

判斷特定的 Windows NT 版本

如果平台已被判定為 Windows NT,主要或次要版本可以加以分析來判斷特定的版本:
//platform is NT
case System::PlatformID::Win32NT:

switch(osInfo->Version->Major)
    {
        case 3:
            Console::WriteLine("Windows NT 3.51");
            break;
        case 4:
            Console::WriteLine("Windows NT 4.0");
            break;
        case 5:
            if (osInfo->Version->Minor==0) 
                Console::WriteLine("Windows 2000");
            else
                Console::WriteLine("Windows XP");
                break;
     }break;
				

建置範例

下列步驟將示範如何建立測試案例中,若要示範這項功能。
  1. 在 Visual 的 Studio.NET 中建立新的受管理的 C + + 應用程式,稱為 determine0S。這會建立簡單的"hello 世界 」 應用程式。按兩下 [方案總管] 視窗中,程式碼編輯器中開啟 DetermineOS.cpp。
  2. 刪除所有 DetermineOS.cpp 中程式碼。
  3. 貼上下列程式碼中的:
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    // This is the entry point for this application
    #ifdef _UNICODE
    int wmain(void)
    #else
    int main(void)
    #endif
    {
       //get OperatingSystem info from the system namespace
       System::OperatingSystem *osInfo = System::Environment::OSVersion;
    
       //is the platform win9x or NT
       switch(osInfo->Platform)
       {
          //platform is win9x
          case System::PlatformID::Win32Windows:
        
             switch (osInfo->Version->Minor)
             {
                  case 0:
                     Console::WriteLine ("Windows 95");
                  break;
                  case 10:
                  if(osInfo->Version->Revision.ToString() == "2222A")
                     Console::WriteLine("Windows 98 Second Edition");
                  else
                     Console::WriteLine("Windows 98");
                  break;
                  case  90:
                     Console::WriteLine("Windows ME");
                  break;
             }
             break;
    			
             //platform is NT
             case System::PlatformID::Win32NT:
    
                switch(osInfo->Version->Major)
                {
                   case 3:
                      Console::WriteLine("Windows NT 3.51");
                   break;
                   case 4:
                      Console::WriteLine("Windows NT 4.0");
                   break;
                   case 5:
                   if (osInfo->Version->Minor==0) 
                      Console::WriteLine("Windows 2000");
                   else
                      Console::WriteLine("Windows XP");
                   break;
                }break;
       }
    
       Console::ReadLine();
       return 0;
    }
    					
  4. 按下 CTRL + F5 執行應用程式。請注意的 Windows 版本會顯示在主控台視窗中。

?考

如需有關 Visual C++.NET 的一般資訊,請造訪下列的 Microsoft Usenet 新聞群組及 Microsoft 網站:
Microsoft.public.dotnet.languages.vc (http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?query=Microsoft.public.dotnet.languages.vc+&dg=&cat=en_US_d02fc761-3f6b-402c-82f6-ba1a8875c1a7&lang=en&cr=&pt=&catlist=&dglist=&ptlist=&exp=&sloc=en-us)

Visual C++ .NET (2002) Support Center (http://support.microsoft.com/default.aspx?xmlid=fh%3ben-us%3bvcnet)

這篇文章中的資訊適用於:
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ 2005 Express Edition
關鍵字:?
kbmt kbhowtomaster kbnewsgrouplink KB307394 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:307394? (http://support.microsoft.com/kb/307394/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。