惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
A
About on SuperTechFans
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
The Cloudflare Blog
F
Fortinet All Blogs
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
罗磊的独立博客
量子位
有赞技术团队
有赞技术团队
V
V2EX
Engineering at Meta
Engineering at Meta

博客园 - 玄新

asp.net 2.0 中加密web.config 文件中的配置节 Windows DNA架构 使用DirectoryServices给文件添加访问权限 - 玄新 - 博客园 使用WMI来获取CPU序列号 - 玄新 - 博客园 什么是WMI C#读取设备信息(源代码下载) 格式化日期型数据 用 javascript显示时间日期代码(来自网上) - 玄新 - 博客园 昨天发现System.Diagnostics.Process 一样的可以打开一个网页的,直接的输入网页的地址就好了 - 玄新 - 博客园 文本框输入限制大全 在b/s开发中经常用到的javaScript技术 文本框里只能输入数字和退格键 (datagridview)如果单击列表头,全选.(第一列为复选框) DataGridView新特色(vs2005) 实现窗体淡入淡出的完整代码 excel 导入导出access数据库(winform) 递归得到叶子节点。 验证手机号码,小灵通号码函数 如何在SQL中对行进行动态编号
如何使用 Visual C# .NET 检查 Windows 版本[转]
玄新 · 2007-01-30 · via 博客园 - 玄新

获取 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 ();
      }
   }
}