日期:2009-07-10  浏览次数:20384 次

感谢jackyoung02(冷雨夜)!!!!




如何:使用 Visual C# .NET 检查 Windows 版本 (Q304283)


--------------------------------------------------------------------------------

本文讨论的内容属于:
Microsoft Visual C# .NET (2002)

--------------------------------------------------------------------------------

如果您想要参考 Microsoft Visual Basic .NET 版本的文章,请参考Q304289。

如果您想要参考 Microsoft Visual C++ .NET 版本的文章,请参考Q307394。

本文内容:

概述
需求
获取 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。

返回
--------------------------------------------------------------------------------

需求
Microsoft Visual C# .NET
对 Visual C# 编程有一定理解
返回
--------------------------------------------------------------------------------

获取 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.Win