擷取 Windows 版本資料
擷取 Windows 系統資訊
判斷平台
判斷 Windows 95, Windows 98, Windows 98 第二版或 Windows Me 的版本
判斷 Windows NT, Windows 2000, 或 Windows XP 的版本
編譯範例
--------------------------------------------------------------------------------
概述
本文描述了如何檢查您的應用運行於哪個作業系統上。本文區分了 Microsoft Windows 95, Microsoft Windows 98, Microsoft Windows 98 第二版, Microsoft Windows Millennium Edition (Windows Me), Microsoft Windows NT 3.51, Microsoft Windows NT 4.0, Microsoft Windows 2000, 和 Microsoft Windows XP。
擷取 Windows 版本資料
為了檢查作業系統,您必須擷取下列資料:
+--------------------------------------------------------------+
| |Windows|Windows|Windows|Windows NT|Windows|Windows|
| | 95 | 98 | Me | 4.0 | 2000 | XP |
+--------------------------------------------------------------+
|PlatformID | 1 | 1 | 1 | 2 | 2 | 2 |
+--------------------------------------------------------------+
|主要版本號 | 4 | 4 | 4 | 4 | 5 | 5 |
+--------------------------------------------------------------+
|副版本號碼 | 0 | 10 | 90 | 0 | 0 | 1 |
+--------------------------------------------------------------+
注釋:儘管本文的代碼在所有 32-bit 版本的 Windows 上驗證過,但 Windows 95 和 Windows NT 3.51 不支援 Microsoft Visual Studio .NET 或者 common language runtime。
擷取 Windows 系統資訊
在 System 命名空間中包含了一個名為 OperatingSystem 的類。在 OperatingSystem 類中的屬性提供了正在使用的作業系統資訊。System.Environment 類中的 OSVersion 屬性返回一個 OperatingSystem 對象。
System.OperatingSystem osInfo = System.Environment.OSVersion;
判斷平台
判斷作業系統的第一步就是辨別正在使用的是哪個作業系統。您可以使用 OperatingSystem 類中的 PlatformID 屬性來決定在用的是哪個作業系統。
例如,枚舉類型屬性 Win32Windows 的值指明了下列作業系統之一:
Windows 95
Windows 98
Windows 98 Second Edition
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, 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# console 應用。系統會預設開啟 Class1.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 ();
}
}
}