Search This Blog

Loading...

May 21, 2011

How to check Disk Space and Size of Disk in System using C#


Sponsored Links:
In C# you can easily check the total size of your disk, available free space and used Space by using following simple  code.

C# Code

using System;
using System.Management;
namespace testApp
{
class Program
{
static void Main(string[] args)
{
string strOccupiedSpace = "";
ManagementObject obj = new
ManagementObject("MyDrive=\"c:\"");
obj.Get();
Console.WriteLine("Disk Size: (bytes) " + obj["Size"]);
Console.WriteLine("Disk Free Space: (bytes) " + obj["FreeSpace"]);
strOccupiedSpace = Convert.ToString(Convert.ToDecimal(obj["Size"]) - 
Convert.ToDecimal(obj["FreeSpace"]));
Console.WriteLine("Disk Occupied Space: (bytes) " + strOccupiedSpace);
Console.ReadLine();
}
}
}


Follow by Email