We are going to discuss how to obtain data from hard disk via WMI. Sometimes we have to understand how much free space there is on our disk, What kind of drive it is and/or what file system is being used.
Not so many times, Perhaps we need to know what the serial number is on the hard disk; especially when we want users to purchase our program licenses. All this will be covered by C#. First of All we know that..
Windows Management Instrumentation (WMI) is the Windows Operating and Data Management (WMI) infrastructure. Writing WMI scripts or apps for remote computer automation of administrative duties, But WMI provides other operating system and product components with management information, too.
For instance, System Center Operations Manager, Microsoft Operations Manager formerly (MOM) or Windows Remote Management (WinRM).
According to Layman's terms this means that via the use of WMI, we can retrieve data that is at the heart of our hardware and / or services.
In our programs, there are two methods of using WMI. We could write queries ( similar to SQL / LINQ queries ), known as WQL to get this information. or we could make use of the System.Management and System.Management.Instrumentation namespaces.
Open Visual Studio and choose a C# Windows Forms Project. Give your choice any name and add 5 buttons. Your layout should look like.
Add the System.Management
Reference to your project, by clicking Project, References, System.Management
.
Include in your code the following namespaces:
using System.Management;
using System.Management.Instrumentation;
To get serial number of hardisk create a method and call that method :
public string GetHardDiskDSerialNumber(string drive)
{
//Check to see if the user provided a drive letter
//If not default it to "C"
if (string.IsNullOrEmpty(drive) || drive == null)
{
drive = "C";
}
//Create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//bind our management object
disk.Get();
//Return the serial number
return disk["VolumeSerialNumber"].ToString();
}
private void BtnGetHardDiskNumber_Click(object sender, EventArgs e)
{
rtxtDiskInformation.Text = "Serial Number of Hard Disk: " + GetHardDiskDSerialNumber("");
}
Here, we check first if there is a valid drive letter provided, otherwise we will be C:\ default. The Win32_LogicalDisk object is then used to acquire the characteristics of the specific disk. At the end we refer to the Property VolumeSerialNumber
, that gives us the serial number of the disk. Finally, on our button click we called this method.
To get size of hardisk create a method and call that method :
public double GetHDDSize(string drive)
{
//Check to see if the user provided a drive letter
//If not default it to "C"
if (string.IsNullOrEmpty(drive) || drive == null)
{
drive = "C";
}
//Create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//Bind our management object
disk.Get();
//Return the HDD's initial size
return Convert.ToDouble(disk["Size"]);
}
private void BtnGetDiskSize_Click(object sender, EventArgs e)
{
double size = 0;
size = Math.Round(GetHDDSize("C") / 1024 / 1024 / 1024);
//1 KB = 1024 - KiloByte
//1 MB = 1024 ^ 2 - MegaByte
//1 GB = 1024 ^ 3 - GigaByte
//1 TB = 1024 ^ 4 - TeraByte
//1 PB = 1024 ^ 5 - PetaByte
//1 EB = 1024 ^ 6 - ExaByte
//1 ZB = 1024 ^ 7 - ZettaByte
//1 YB = 1024 ^ 8 - YottaByte
//1 BB = 1024 ^ 9 - BrontoByte
rtxtDiskInformation.Text = "Hard Disk Size = " + size.ToString() + " GB";
}
The code of the function is almost identical to the GetHDSerialNo function. We only make the difference in our function by utilizing the Size parameter. Just a note, all the forthcoming functions work the same way, So we are not going to go into too many details of the methodology used in these functions.
There are no rounds off the resulting numbers. We don't have KB, MB, GB or even TB displayed either. This logic must be included by us.
Fortunately it's pretty easy! All we have to do is divide the result by 1024 times (byte size). Three times we divided it and in Gigabytes it gave us the right result.
Many people don't know the level of these sizes, so we have included them here. To be just interesting :)
To get free space of hardisk, create a method and call that method:
public double GetHDDFreeSpace(string drive)
{
//Check to see if the user provided a drive letter
//If not default it to "C"
if (string.IsNullOrEmpty(drive) || drive == null)
{
drive = "C";
}
//Create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//Bind our management object
disk.Get();
//Return the HDD's FreeSpace
return Convert.ToDouble(disk["FreeSpace"]);
}
private void BtnGetFreeSpace_Click(object sender, EventArgs e)
{
double size = 0;
size = Math.Round(GetHDDFreeSpace("C") / 1024 / 1024 / 1024);
//1 KB = 1024 - KiloByte
//1 MB = 1024 ^ 2 - MegaByte
//1 GB = 1024 ^ 3 - GigaByte
//1 TB = 1024 ^ 4 - TeraByte
//1 PB = 1024 ^ 5 - PetaByte
//1 EB = 1024 ^ 6 - ExaByte
//1 ZB = 1024 ^ 7 - ZettaByte
//1 YB = 1024 ^ 8 - YottaByte
//1 BB = 1024 ^ 9 - BrontoByte
rtxtDiskInformation.Text = "Hard Disk Free Space = " + size.ToString() + " GB";
}
It works exactly like Size, but the FreeSpace property is utilized.
To get hard disk type create a method for getting the drive type and call that method. same as above.
private string GetDriveType(string drive)
{
//Check to see if the user provided a drive letter
//If not default it to "C"
if (string.IsNullOrEmpty(drive) || drive == null)
{
drive = "C";
}
//Create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//Bind our management object
disk.Get();
//Return the HDD's FreeSpace
return disk["DriveType"].ToString();
}
private void BtnDriveType_Click(object sender, EventArgs e)
{
string strDriveType = null;
switch (GetDriveType("C"))
{
case "0":
strDriveType = "Unknown";
break;
case "1":
strDriveType = "Readable";
break;
case "2":
strDriveType = "Writable";
break;
case "3":
strDriveType = "Read / Write Supported";
break;
case "4":
strDriveType = "Write Once";
break;
}
rtxtDiskInformation.Text = "Hard Disk Drive Type = " + strDriveType;
}
To get hard disk file system create a method for getting the file system and call that method. same as above.
private string GetDiskFileSystem(string drive)
{
//Check to see if the user provided a drive letter
//If not default it to "C"
if (string.IsNullOrEmpty(drive) || drive == null)
{
drive = "C";
}
//Create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
//Bind our management object
disk.Get();
//Return the HDD's FreeSpace
return disk["FileSystem"].ToString();
}
private void BtnFileSystem_Click(object sender, EventArgs e)
{
rtxtDiskInformation.Text = "Hard Disk File System = " + GetDiskFileSystem("C");
}
Thats it!