现在的位置: 首页 > 综合 > 正文

使用 Visual C# .Net 确定 Windows 版本

2012年08月15日 ⁄ 综合 ⁄ 共 3769字 ⁄ 字号 评论关闭
文章目录

概要

本文逐步介绍如何确定应用程序所在的系统上使用的是哪一种操作系统。本文还将说明 Microsoft Windows 95、Microsoft Windows 98、Microsoft Windows 98 Second Edition、Microsoft Windows Millennium Edition (Windows Me)、Microsoft Windows NT 3.51、Microsoft Windows NT 4.0、Microsoft Windows 2000 及 Microsoft 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     |
+--------------------------------------------------------------+
|Major      |       |       |       |          |       |       |
| 版本   | 4     | 4     | 4     | 4        | 5     | 5     |
+--------------------------------------------------------------+
|Minor      |       |       |       |          |       |       |
| 版本   | 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 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 Second Edition 或 Windows Me 的具体版本

如果确定了平台是 Windows 95、Windows 98、Windows 98 Second Edition 或 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;

生成示例

下面的步骤生成一个演示此功能的测试方案:

1. 在 Visual Studio .NET 中,打开一个新的 C# 控制台应用程序。默认情况下将打开 Class1.cs 的代码窗口。
2. 将 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();
      }
   }
}

3. 按 CTRL+F5 组合键以运行该应用程序。请注意,Windows 版本将在控制台窗口中出现。


补充:System.Environment.SystemDirectory 获取系统目录的完全限定路径,即C:/WINDOWS/system32    (by jinru2560)

抱歉!评论已关闭.