本文將逐步告訴您,如何判斷哪一個作業系統正在執行應用程式的系統上的使用中。這份文件是由 Microsoft Windows 95、 Microsoft Windows 98、 Microsoft Windows 98 第二版、 Microsoft Windows 千禧版 (Windows Me)、 Microsoft Windows NT 3.51、 Microsoft Windows NT 4.0、 Microsoft Windows 2000 及 Microsoft Windows XP 所區分。
需求
- Microsoft Visual C#.NET
- Microsoft Visual C# 2005年或 Visual C# 2008
- Visual C# 程式設計的中間層級的了解
取得 Windows 版本資料
若要判斷系統執行的作業系統,您必須取得下列資料:
+--------------------------------------------------------------+
| |Windows|Windows|Windows|Windows NT|Windows|Windows|
| | 95 | 98 | Me | 4.0 | 2000 | XP |
+--------------------------------------------------------------+
|PlatformID | 1 | 1 | 1 | 2 | 2 | 2 |
+--------------------------------------------------------------+
|Major | | | | | | |
| version | 4 | 4 | 4 | 4 | 5 | 5 |
+--------------------------------------------------------------+
|Minor | | | | | | |
| version | 0 | 10 | 90 | 0 | 0 | 1 |
+--------------------------------------------------------------+
注意: 雖然本文的程式碼驗證的所有 32 位元版本的 Windows 搭配使用,Windows 95 與 Windows NT 3.51 不支援 Microsoft Visual Studio.NET 或公用語言執行時間。
取得作業系統資訊
System 命名空間包含名為
OperatingSystem 的類別。
OperatingSystem 類別的屬性提供必要的資訊有關使用中的作業系統。
System.Environment 類別的 [
OSVersion 屬性傳回
OperatingSystem 物件。
System.OperatingSystem osInfo = System.Environment.OSVersion;
決定平台
OperatingSystem 資訊的邏輯評估之第一個步驟是判斷哪一個平台為使用中。
OperatingSystem 類別的
PlatformID 屬性用於判斷哪些平台為使用中。
比方說
Win32Windows 屬性的列舉的值會指示下列作業系統其中:
- Windows 95
- Windows 98
- Windows 98 第二版
- Windows Me
同樣地,
WinNT 屬性指示其中一種下列作業系統:
- Windows NT 3.51
- Windows NT 4.0
- Windows 2000
- Windows XP
switch(osInfo.Platform)
{
case System.PlatformID.Win32Windows:
{
// Code to determine specific version of Windows 95,
// Windows 98, Windows 98 Second Edition, or Windows Me.
}
case System.PlatformID.Win32NT:
{
// Code to determine specific version of Windows NT 3.51,
// Windows NT 4.0, Windows 2000, or Windows XP.
}
}
判斷特定版本的 Windows 95、 Windows 98、 Windows 98 第二版或 Windows Me
如果您決定平台是 Windows 95、 Windows 98、 Windows 98 第二版或 Windows Me,您可以分析主要或次要版本],以決定特定的版本。
// Platform is Windows 95, Windows 98, Windows 98 Second Edition,
// or Windows Me.
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 2000 或 Windows XP
如果您決定平台是 Windows NT 3.51、 Windows NT 4.0、 Windows 2000 或 Windows XP,您可以分析主要或次要版本],以決定特定的版本。
// Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,
// or Windows XP.
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;
建置範例
下列步驟建置示範這項功能的測試案例:
- 在 Visual 的 Studio.NET 開啟新 C# 主控台應用程式。預設開啟 Class1.cs 程式碼視窗。
附註在 Visual Studio 2005 或 Visual Studio 2008 中,預設檔案會是 Program.cs。您可以按兩下 Program.cs 以開啟 [程式碼] 視窗。 - 下列範例程式碼替換 Class1.cs 程式碼編輯器視窗中程式碼的所有:
using System;
namespace determineOS_CS
{
class Class1
{
static void Main(string[] args)
{
// Get OperatingSystem information from the system namespace.
System.OperatingSystem osInfo =System.Environment.OSVersion;
// Determine the platform.
switch(osInfo.Platform)
{
// Platform is Windows 95, Windows 98,
// Windows 98 Second Edition, or Windows Me.
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 Windows NT 3.51, Windows NT 4.0, Windows 2000,
// or Windows XP.
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 ();
}
}
}
- 按下 CTRL + F5 執行應用程式。請注意 Windows 版本會顯示在主控台視窗中。
文章編號: 304283 - 上次校閱: 2008年7月15日 - 版次: 3.0
這篇文章中的資訊適用於:
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# 2005
- Microsoft Visual C# 2008 Express Edition
| kbmt kbhowtomaster kbprod2web KB304283 KbMtzh |
機器翻譯重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:
304283
(http://support.microsoft.com/kb/304283/en-us/
)
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。