Click here to Skip to main content
12,244,103 members (51,246 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

89.3K views
3.4K downloads
79 bookmarked
Posted

Easily Get and Compare OS Version Information

, 30 Mar 2010 CPOL
A couple of classes to make checking the host OS version easy and error-free
OSVersionDemo.exe
OSVersionDemo.exe
OSVersionDemo.exe
Common.OSVersion.dll
Common.OSVersion.dll
Common.OSVersion.dll
OSVersion
OSVersionDemo
bin
Release
Common
Icons
Smiley.ico
OSVersionDemo.csproj.user
OSVersionLib
bin
Release
Common
OSVersionLib.csproj.user
OSVersionLib.Debug.FxCop
OSVersionLib.snk
OSVersionLibTest
bin
Release
OSVersionLibTest.csproj.user
Debug
Smiley.ico
obj
Debug
Refactor
temp
TempPE
Release
temp
TempPE
Debug
obj
Debug
temp
TempPE
Release
temp
TempPE
OSVersionLib.Debug.FxCop
OSVersionLib.snk
Debug
obj
Debug
temp
TempPE
Release
temp
TempPE
Smiley.ico
OSVersionLib.snk
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

using Common;

namespace OSVersionDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox _OS1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox _Text1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.CheckBox _Equals;
		private System.Windows.Forms.CheckBox _NotEquals;
		private System.Windows.Forms.CheckBox _MoreThanOrEqual;
		private System.Windows.Forms.CheckBox _LessThanOrEqual;
		private System.Windows.Forms.CheckBox _LessThan;
		private System.Windows.Forms.CheckBox _MoreThan;
		private System.Windows.Forms.TextBox _Text2;
		private System.Windows.Forms.ComboBox _OS2;

		OperatingSystemVersion _Host = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			try
			{
				_Host = OperatingSystemVersion.Current;
				Text += " " + _Host.OSArchitecture;
			}
			catch ( Exception x )
			{
				MessageBox.Show( x.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error );
				Close();
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this._OS1 = new System.Windows.Forms.ComboBox();
			this._Text1 = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this._Equals = new System.Windows.Forms.CheckBox();
			this._NotEquals = new System.Windows.Forms.CheckBox();
			this._MoreThanOrEqual = new System.Windows.Forms.CheckBox();
			this._LessThanOrEqual = new System.Windows.Forms.CheckBox();
			this._LessThan = new System.Windows.Forms.CheckBox();
			this._MoreThan = new System.Windows.Forms.CheckBox();
			this._Text2 = new System.Windows.Forms.TextBox();
			this._OS2 = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// _OS1
			// 
			this._OS1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this._OS1.Location = new System.Drawing.Point(8, 8);
			this._OS1.Name = "_OS1";
			this._OS1.Size = new System.Drawing.Size(121, 21);
			this._OS1.TabIndex = 0;
			this._OS1.SelectedIndexChanged += new System.EventHandler(this._OS1_SelectedIndexChanged);
			// 
			// _Text1
			// 
			this._Text1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this._Text1.Location = new System.Drawing.Point(8, 40);
			this._Text1.Name = "_Text1";
			this._Text1.ReadOnly = true;
			this._Text1.Size = new System.Drawing.Size(272, 20);
			this._Text1.TabIndex = 1;
			this._Text1.TabStop = false;
			this._Text1.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Location = new System.Drawing.Point(8, 72);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(272, 8);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			// 
			// groupBox2
			// 
			this.groupBox2.Location = new System.Drawing.Point(8, 136);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(272, 8);
			this.groupBox2.TabIndex = 9;
			this.groupBox2.TabStop = false;
			// 
			// _Equals
			// 
			this._Equals.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this._Equals.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._Equals.Location = new System.Drawing.Point(16, 88);
			this._Equals.Name = "_Equals";
			this._Equals.Size = new System.Drawing.Size(48, 24);
			this._Equals.TabIndex = 3;
			this._Equals.TabStop = false;
			this._Equals.Text = "==";
			// 
			// _NotEquals
			// 
			this._NotEquals.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this._NotEquals.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._NotEquals.Location = new System.Drawing.Point(16, 112);
			this._NotEquals.Name = "_NotEquals";
			this._NotEquals.Size = new System.Drawing.Size(48, 24);
			this._NotEquals.TabIndex = 4;
			this._NotEquals.TabStop = false;
			this._NotEquals.Text = "!=";
			// 
			// _MoreThanOrEqual
			// 
			this._MoreThanOrEqual.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this._MoreThanOrEqual.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._MoreThanOrEqual.Location = new System.Drawing.Point(112, 112);
			this._MoreThanOrEqual.Name = "_MoreThanOrEqual";
			this._MoreThanOrEqual.Size = new System.Drawing.Size(48, 24);
			this._MoreThanOrEqual.TabIndex = 6;
			this._MoreThanOrEqual.TabStop = false;
			this._MoreThanOrEqual.Text = ">=";
			// 
			// _LessThanOrEqual
			// 
			this._LessThanOrEqual.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this._LessThanOrEqual.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._LessThanOrEqual.Location = new System.Drawing.Point(112, 88);
			this._LessThanOrEqual.Name = "_LessThanOrEqual";
			this._LessThanOrEqual.Size = new System.Drawing.Size(48, 24);
			this._LessThanOrEqual.TabIndex = 5;
			this._LessThanOrEqual.TabStop = false;
			this._LessThanOrEqual.Text = "<=";
			// 
			// _LessThan
			// 
			this._LessThan.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this._LessThan.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._LessThan.Location = new System.Drawing.Point(208, 88);
			this._LessThan.Name = "_LessThan";
			this._LessThan.Size = new System.Drawing.Size(48, 24);
			this._LessThan.TabIndex = 7;
			this._LessThan.TabStop = false;
			this._LessThan.Text = "<";
			// 
			// _MoreThan
			// 
			this._MoreThan.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
			this._MoreThan.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._MoreThan.Location = new System.Drawing.Point(208, 112);
			this._MoreThan.Name = "_MoreThan";
			this._MoreThan.Size = new System.Drawing.Size(48, 24);
			this._MoreThan.TabIndex = 8;
			this._MoreThan.TabStop = false;
			this._MoreThan.Text = ">";
			// 
			// _Text2
			// 
			this._Text2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this._Text2.Location = new System.Drawing.Point(8, 192);
			this._Text2.Name = "_Text2";
			this._Text2.ReadOnly = true;
			this._Text2.Size = new System.Drawing.Size(272, 20);
			this._Text2.TabIndex = 11;
			this._Text2.TabStop = false;
			this._Text2.Text = "";
			// 
			// _OS2
			// 
			this._OS2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this._OS2.Location = new System.Drawing.Point(8, 160);
			this._OS2.Name = "_OS2";
			this._OS2.Size = new System.Drawing.Size(121, 21);
			this._OS2.TabIndex = 10;
			this._OS2.SelectedIndexChanged += new System.EventHandler(this._OS2_SelectedIndexChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 222);
			this.Controls.Add(this._Equals);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this._Text1);
			this.Controls.Add(this._OS1);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this._NotEquals);
			this.Controls.Add(this._MoreThanOrEqual);
			this.Controls.Add(this._LessThanOrEqual);
			this.Controls.Add(this._LessThan);
			this.Controls.Add(this._MoreThan);
			this.Controls.Add(this._Text2);
			this.Controls.Add(this._OS2);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "OSVersionDemo";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.DoEvents();

			Application.Run( new Form1() );
		}

		private void Form1_Load( object sender, System.EventArgs e )
		{
			Populate( _OS1 );
			Populate( _OS2 );

			_OS1.SelectedIndex = 0;
			_OS2.SelectedIndex = 0;
		}

		private void Populate( ComboBox cb )
		{
			ArrayList a = new ArrayList();

			cb.Items.Add( "This host" );

			foreach ( OSVersion v in Enum.GetValues( typeof( OSVersion ) ) )
//				cb.Items.Add( new CBData( Enum.GetName( typeof( OSVersion), v ), v ) );
				cb.Items.Add( v );
		}

		private void _OS1_SelectedIndexChanged( object sender, System.EventArgs e )
		{
			_Text1.Text = String.Empty;

			object o = _OS1.SelectedItem;
			if ( o == null ) return;

			OSVersionInfo v = GetOSVersionInfo( o );
			_Text1.Text = v.ToString();

			Compare();
		}

		private void _OS2_SelectedIndexChanged( object sender, System.EventArgs e )
		{
			_Text2.Text = String.Empty;

			object o = _OS2.SelectedItem;
			if ( o == null ) return;

			OSVersionInfo v = GetOSVersionInfo( o );
			_Text2.Text = v.ToString();

			Compare();
		}

		private OSVersionInfo GetOSVersionInfo( object o )
		{
			if ( o is string ) return _Host;

			OSVersion v = ( OSVersion ) o;

			return OSVersionInfo.GetOSVersionInfo( v );
		}

		private void Compare()
		{
			object o1 = _OS1.SelectedItem;
			object o2 = _OS2.SelectedItem;

			if ( o1 == null || o2 == null ) return;

			OSVersionInfo v1 = GetOSVersionInfo( o1 );
			OSVersionInfo v2 = GetOSVersionInfo( o2 );

			_Equals.Checked          = ( v1 == v2 );
			_NotEquals.Checked       = ( v1 != v2 );
			_LessThanOrEqual.Checked = ( v1 <= v2 );
			_MoreThanOrEqual.Checked = ( v1 >= v2 );
			_LessThan.Checked        = ( v1 <  v2 );
			_MoreThan.Checked        = ( v1 >  v2 );
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nicholas Butler
United Kingdom United Kingdom

I built my first computer, a Sinclair ZX80, on my 11th birthday in 1980.
In 1992, I completed my Computer Science degree and built my first PC.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing concurrent software professionally, using multi-processor machines, since 1995.

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

If you would like help with multithreading, please contact me via my website:


I can work 'virtually' anywhere!

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 30 Mar 2010
Article Copyright 2005 by Nicholas Butler
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid