Click here to Skip to main content
Click here to Skip to main content

Reusable System Information Component

, 2 Sep 2002
Rate this:
Please Sign up or sign in to vote.
A reusable .NET component, which retrieves the operating system information.

Introduction

In this article we will create a reusable component which retrieves the operating system information. We will explore ComponentModel and SystemInformation class and also we will use this component in different clients like Win Forms, Web Forms & Console Application. SystemInformation class (alias System.Windows.Forms.SystemInformation). This class provides static methods and properties that can be used to get information such as operating system settings, network availability, and the capabilities of hardware installed on the system. One point to remember is SystemInformation class cannot be instantiated. So we are going to create a component which is derived from System.ComponentModel.Component the default implementation of IComponent. So why IComponent ?. IComponent serves as the base class for all components in the common language runtime and IComponent allows a component to keep track of design-time information, such as its container component or its name, or to access services that the designer may expose.Component class is remotable and derives from MarshalByRefObject so which Enables access to objects across application domain boundaries in applications that support remoting.

In below figure is the model how component is accessed by different client applications.  

Lets discuss first about SysInfo component, SysInfo Class is derived from Component class of .net frame work and this component will have read properties for getting the system information. So I have selected most important methods of SystemInformation class methods and implemented as get properties.

Namespace:

  • SysInfoLib

Class:

  • SysInfo

Properties

  • UserName (get property)
  • ComputerName (get property)
  • Network (get property)
  • MonitorInfo (get property)
  • MouseInfo (get property)
  • BootInfo (get property) 

The main purpose of SysInfo component is to encapsulate the intricacies of retrieving the System information so I tried to implement all the properties to return the information as string ,This will give understandable knowledge out put from the properties. For example the BootInfo property, Which need to do some checks to know how OS was booted. In order to do that we need to check all the options of BootMode enum. Similarly all other properties of the component are implemented.

Here is the code for your reference.  

//Sysinfo.cs 
using System.Windows.Forms; 
using System.ComponentModel; 
namespace SysInfoLib 
{ 
public class SysInfo: Component{ 
public SysInfo(){} 
public string UserName 
   { 
      get 
      { 
         return SystemInformation.UserName; 
      } 
   } 
  
 public string ComputerName 
   { 
      get 
      { 
         return SystemInformation.ComputerName; 
      } 
   } 
  
 public string Network 
   { 
      get 
      { 
         if(SystemInformation.Network) 
         return "Connected"; 
         else 
         return "Disconnected"; 
             
      } 
   } 
 public string MonitorInfo 
   { 
      get 
      { 
         return SystemInformation.MonitorCount.ToString(); 
      } 
   } 
  
 public string MouseInfo 
   { 
      get 
      { 
            if(SystemInformation.MousePresent) 
          { 
           return SystemInformation.MouseButtons.ToString() + " Button Mouse"; 
           } 
          else 
          return "No Mouse Connected"; 
      } 
   } 
 public string BootInfo 
   { 
      get 
      { 
          if(SystemInformation.BootMode==BootMode.Normal) 
            return "Normal"; 
          else if(SystemInformation.BootMode==BootMode.FailSafe) 
            return "Started In Safe Mode"; 
          else if(SystemInformation.BootMode==BootMode.FailSafeWithNetwork) 
             return "Started In Safe Mode with Network Support"; 
          else  
             return "No info"; 
        } 
    } 
  } 
 } 

Compile the above code as Component Library (In Frame work SDK  : CSC /target:library SysInfo.cs ), which will generate .dll file.

Now lets see small client code snippets for using the SysInfo component. As per the above model we can use the SysInfo component in different client like console application, Win Form Client, Web Form Client.

So to use this component we will simply create the instance of the component and use its properties to retrieve the system information.

//sysConClient.cs  Console Client Application 
using System; 
using SysInfoLib; //using the System info reference library  
public class sysConClient 
{ 
 public static void Main() 
 { 
   SysInfo s=new SysInfo(); 
   Console.WriteLine(s.UserName); 
   Console.WriteLine(s.ComputerName);    
   Console.WriteLine(s.BootInfo); 
   Console.WriteLine(s.Network); 
   Console.WriteLine(s.MonitorInfo); 
   Console.WriteLine(s.MouseInfo); 
   } 
  } 

In Win Form clients example we will display the system information when user clicks the button. So in our example I used button click event to generate a refresh event which will in turn calls the Paint event of the Win Form where we will create the instance and display the system information using Graphics class DrawString method.

//sysFrmCln.cs Win Form Client snippet 
// To avoid the paint event always paints whenever form refreshes we will 
// use bool variable to call only when button click event is called 
private static bool bStat=false; 
  
// Click event will call the refersh method which in turn generates 
// OnPaint event 
private void button1_Click(object sender, EventArgs e)  
{ 
   bStat=true; 
   this.Refresh();     
 } 
  
// We will override the OnPaint event and when user click button system 
// information is displayed 
protected override void OnPaint(PaintEventArgs e) { 
  if(bStat) 
  { 
   SysInfo s=new SysInfo(); 
   string sysStr="User Name: "+s.UserName; 
   sysStr+="\nComputer Name: "+s.ComputerName; 
   sysStr+="\nBoot Information: "+s.BootInfo; 
   sysStr+="\nNetwork Information: "+s.Network; 
   sysStr+="\nMonitor Information: "+s.MonitorInfo; 
   sysStr+="\nMouse Information: "+s.MouseInfo; 
   Graphics g = e.Graphics; 
   g.DrawString(sysStr, this.Font, new SolidBrush(Color.Black), 10,100); 
   s.Dispose(); 
   bStat=false; 
  } 
} 
 

For the Web Form client import the library and create a instance of SysInfo component and using Respone.Write display the system information.

So now you can add more properties to the SysInfo component by using other member functions of SystemInformation class.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Chandra Hundigam
Architect
United States United States
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

Comments and Discussions

 
Questionhow to use sysinfo to get client info Pinmemberlshirota15-Nov-05 19:51 
Question?Component.Parent? Pinmemberallancto15-May-03 15:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 3 Sep 2002
Article Copyright 2002 by Chandra Hundigam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid