Windows Management Instrumentation in C# Part-01

No.of Views1584
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : CSharp , WMI
Windows Management Instrumentation in C# Part-01
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I saw at the message board in codegain.com, the most of the time lot of developers asked question  about Windows Management Instrumentation. Actually Windows Management Instrumentation is an access Windows Base operating system infrastructure for management data and operations.more information about WMI

To this we need write script to access objects and properties to get values or data from  the Operating System level. And .NET Framework 2.0 or later versions has fully query support to access the data with WMI.According this i woudl like to write a series of article to play with WMI(Windows Management Instrumentation) data and properties.

This is the Part-01 of the series and going to eloborate get the hard disk information using WMI query with .NET framework 3.5  class.

Implementation

first we  need create a  console application using Visual Studio 2005 or 2008 give project name as  "CG.CS.WMI".In the .net framework class libaray has a collection of classes for the WMI under the System.Management.

Since add that  reference to the created project.To this right click on the project and then select add reference option from the menu. It's bring a  window with tab collection under the .NET have to select ehe System.Management libarary. To more clear look followings figure.

Add reference to the project

Now we have addeed reference to project. now you can write code to access Management objects and properties.To this create a class to keep all Hard disk information gethering code in a class. For that right click on the project and then select Add new item menu.from that select new class item and give th class name as "WMIUtility.CS". within this class add a using statement in the top of the class to the access WMI objects.

{codecitation class="brush: csharp; gutter: true;" width="500px"}

using System.Management;

{/codecitation}

We could get lot's of properties value using WMI to the hard disk, but here i 'm going to access  only most important properties.Such as serialno, freesize and size of the a specific drive.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

//Code RRaveen (vrrave[at]codegain.com)
//Offical site: http://codegain.com

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with this program.  If not, see .


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;


namespace CG.CS.Utils.WMI
{
///


/// This is support to play wiht Windows Management intrusument.
///
public class WMIUtility
{
public WMIUtility()
{

}

///
/// Get logical volume serial no in Hard disk
///
///
///
public static string GetDriveSerialNO(string logicaldriveName)
{
if (string.IsNullOrEmpty(logicaldriveName))
{
logicaldriveName = "C";
}
// create management object to invoke the WMI query to access the drive.
ManagementObject hdDisk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + logicaldriveName + ":\"");
hdDisk.Get();
// read the volume serial no value using key from the collections
string serialNo = hdDisk["VolumeSerialNumber"].ToString();
// fianlly dispose the management object.
hdDisk.Dispose();
return serialNo;
}


public static double GetHDDiskFreeSpace(string logicaldriveName)
{

if (string.IsNullOrEmpty(logicaldriveName))
{
logicaldriveName = "C";
}
// create management object to invoke the WMI query to access the drive.
ManagementObject hdDisk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + logicaldriveName + ":\"");
hdDisk.Get();
// read the freespace value using key   from the collections
double freeSize = Convert.ToDouble(hdDisk["FreeSpace"]);
// fianlly dispose the management object.
hdDisk.Dispose();
return freeSize;
}

///
/// Get size of the drive
///
///
///
public static double GetHDDiskSize(string logicaldriveName)
{
if (string.IsNullOrEmpty(logicaldriveName))
{
logicaldriveName = "C";
}
// create management object to invoke the WMI query to access the drive.
ManagementObject hdDisk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + logicaldriveName + ":\"");

hdDisk.Get();
// read the size value using key   from the collections
double size = Convert.ToDouble(hdDisk["Size"]); //it's return the byte
size = (size / (1024 * 1024)); // now it;s MB
return size;
}
}
}

{/codecitation}

In above class has three methods and most of the code are code line are similar, but here we need to understand followig block.

{codecitation class="brush: csharp; gutter: true;" width="500px"}

// create management object to invoke the WMI query to access the drive.
ManagementObject hdDisk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + logicaldriveName + ":\"");

hdDisk.Get();

{/codecitation}

In the first line create new instance to Managementobject to  execute the WC query.And then within the constructor i used the "Win32_LogicalDisk.DeviceID=\"" + logicaldriveName + ":\"".It's called WS script to  get the drive object prooerties which are under the Win32_lofical table.and if Device ID is equal to which is send as argument from the called program.finally add the ":\" mark to make drive name like "C:" .

finally execute the WS script calling Get() method to object. and then read the values using keys from the collections.

How to invoke this code in your application.

for example If you are going to get C drive size, Just add followings lines in your code.

{codecitation class="brush: csharp; gutter: true;" width="500px"}

double SizeOfDrive= WMIUtility.GetHDDiskSize("C");

{/codecitation}

Conclusion

That's all i hope its help   to you get Hard disk information using .NET Framework.in the next article i going to get CPU information using WMI with C#.

Thank you

RRaveen


 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top