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:
Class:
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.
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.
using System;
using SysInfoLib;
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.
private static bool bStat=false;
private void button1_Click(object sender, EventArgs e)
{
bStat=true;
this.Refresh();
}
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.