How to measure hard disk capacity?

  • Thread starter Thread starter AlirezaH
  • Start date Start date
AlirezaH,

I would perform a WMI query for the instance of Win32_PhysicalMedia that
represents the actual drive and then check the Capacity property on it. You
can use the classes in the System.Management namespace to access this data.

Hope this helps.
 
AlirezaH said:
How to measure hard disk capacity?

This will do a little more:

using System;
using System.Management;

namespace sample {

/// <summary>
/// Displays disk space information for all connected hard drives.
/// </summary>
class freeSpace {

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main() {
string query = "select name, FreeSpace, Size from win32_logicaldisk
where drivetype=3";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

foreach(ManagementObject manobj in searcher.Get()) {
double freespace2 = double.Parse(manobj["FreeSpace"].ToString());
double disksize2 = double.Parse(manobj["Size"].ToString());
double pctfree2 = freespace2 / disksize2;
int lengthdiff = disksize2.ToString("N0").Length -
freespace2.ToString("N0").Length;

Console.WriteLine("For drive {0}", manobj["name"]);
Console.WriteLine("\tDisk size: " + disksize2.ToString("N0") + "
bytes");

/// <summary>
/// Format the free space line so the number lines up
/// right-justified with the disk size number
/// when using a fixed-width font.
/// </summary>
switch (lengthdiff) {
case 0:
Console.WriteLine("\tFree space: " + freespace2.ToString("N0") +
" bytes");
break;
case 1:
Console.WriteLine("\tFree space: " + freespace2.ToString("N0") +
" bytes");
break;
case 2:
Console.WriteLine("\tFree space: " + freespace2.ToString("N0")
+ " bytes");
break;
case 3:
Console.WriteLine("\tFree space: " + freespace2.ToString("N0")
+ " bytes");
break;
default :
Console.WriteLine("\tFree space: " + freespace2.ToString("N0") +
" bytes");
break;
}
Console.WriteLine();

Console.WriteLine("\tPercent free: " + pctfree2.ToString("P"));
Console.WriteLine();
Console.WriteLine();
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top