使用 Visual C# .Net 确定 Windows 版本
2006-03-02 16:09
330 查看
概要
本文逐步介绍如何确定应用程序所在的系统上使用的是哪一种操作系统。本文还将说明 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 对象。[code]System.OperatingSystem osInfo = System.Environment.OSVersion;
确定平台
对 OperatingSystem 信息进行逻辑评估的第一步是确定正在使用哪一种平台。可以使用 OperatingSystem 类的 PlatformID 属性确定在使用哪一种平台。例如,Win32Windows 属性的枚举值指明下列操作系统之一:
• | Windows 95 |
• | Windows 98 |
• | Windows 98 Second Edition |
• | Windows Me |
• | 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,则可分析主版本还次版本来确定具体版本。[code]//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;
[/code]
确定 Windows NT、Windows 2000 或 Windows XP 的具体版本
如果您确定平台是 Windows NT 3.51、Windows NT 4.0、Windows 2000 或 Windows XP,则可通过分析主版本或次版本来确定具体版本。[code]// 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;
[/code]
生成示例
下面的步骤生成一个演示此功能的测试方案:1. | 在 Visual Studio .NET 中,打开一个新的 C# 控制台应用程序。默认情况下将打开 Class1.cs 的代码窗口。 |
2. | 将 Class1.cs 代码编辑器窗口中的所有代码替换为下列示例代码: [code]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(); } } } [/code] |
3. | 按 CTRL+F5 组合键以运行该应用程序。请注意,Windows 版本将在控制台窗口中出现。 |
补充:System.Environment.SystemDirectory 获取系统目录的完全限定路径,即C:/WINDOWS/system32 (by jinru2560)
相关文章推荐
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 使用 Visual C# .NET 检查 Windows 版本
- 如何使用 Visual C# .NET 检查 Windows 版本[转]
- 70-316 使用Microsoft Visual C# .NET and Microsoft Visual Studio .NET 开发及实现基于Windows的应用程序 考点整理
- 摘:如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页(转载)
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页?
- 如何使用 AllowPartiallyTrustedCallers 属性来调用具有一个强名称从 Web 页通过使用 Visual C#.NET、 Visual C# 2005年或更高版本的程序集
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- 如何使用 Visual C# .NET 对 DataGrid Windows 控件执行分页
- CLR/.NET/C#/Visual Studio/ASP.NET各版本之间的关系(转)
- 使用 SQL Server 2005 Compact Edition 和 Visual C#.NET 快速开发应用程序
- HOWTO:在 Visual C# .NET 中使用自动化创建 Excel 宏
- 使用 GetOleDbSchemaTable 和 Visual C# .NET 检索架构信息-----http://support.microsoft.com/kb/309681/zh-cn#
- HOW TO:使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告