Use Visual C #. Net to determine the Windows version

Source: Internet
Author: User

This document describes how to determine the operating system on which the application is located. This article also describes Microsoft Windows 95, Microsoft Windows 98, Microsoft Windows 98 Second edition, Microsoft Windows Millennium Edition (Windows ME) differences between Microsoft Windows NT 3.51, Microsoft Windows NT 4.0, Microsoft Windows 2000, and Microsoft XP.

Requirements
Microsoft Visual C #. net
Moderate Visual C # programming knowledge

Obtain Windows Data

To determine the operating system running on the system, you must obtain the following data:

+ Platform + | Windows NT | Windows | 95 | 98 | Me | 4.0 | 2000 | XP | + ---------------------------------------------------------- + | platformid | 1 | 1 | 1 | 2 | 2 | + ---------------------------------------------------------------- + | major | version | 4 | 4 | 4 | 5 | 5 | + cores + | minor | version | 0 | 10 | 90 | 0 | 0 | 1 | + -------------------------------------------------------------- +

Remarks: The code in this article has been verified for all 32-bit Windows versions. However, Windows 95 and Windows NT 3.51 do not support Microsoft Visual Studio. net or public Language Runtime Library.

Obtain operating system information

SystemThe namespace containsOperatingsystem.OperatingsystemClass properties provide necessary information about the operating system in use.System. EnvironmentClassOsversionProperty returnOperatingsystemObject.

System.OperatingSystem osInfo = System.Environment.OSVersion;

Determine Platform

PairOperatingsystemThe first step in logical evaluation is to determine which platform is being used. AvailableOperatingsystemClassPlatformidAttribute determines which platform to use.

For example,Win32windowsThe enumerated value of the property indicates one of the following operating systems:

Windows 95
Windows 98
Windows 98 Second Edition
Windows ME

Similarly,WinntAttribute indicates one of the following operating systems:

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.               }         }

Determine the version of Windows 95, Windows 98, Windows 98 Second edition, or Windows ME

If you confirm that the platform is Windows 95, Windows 98, Windows 98 Second edition, or Windows ME, you can analyze the previous version of the main version to determine the specific version.

//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");elseConsole.WriteLine ("Windows 98");break;case 90:Console.WriteLine("Windows Me");break;}break;

Determine the specific version of Windows NT, Windows 2000, or Windows XP

If you are sure that the platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, or Windows XP, you can determine the specific version by analyzing the main version or next version.

// 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");elseConsole.WriteLine("Windows XP");break;}break;

Generation example

The following steps generate a test scheme to demonstrate this function:

1. Open a new C # console application in Visual Studio. NET. By default, the class1.cs code window is opened.
2. Replace all the code in the class1.cs code editor window with the following sample 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");elseConsole.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");elseConsole.WriteLine("Windows XP");break;}break;         }Console.ReadLine();      }   }}

3. Press Ctrl + F5 to run the application. Note that the Windows version will appear in the console window.

Supplement: system. environment. systemdirectory gets the fully qualified path of the system directory, that is, C:/Windows/system32 (by jinru2560)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.