Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm looking for a way to find the total size of a harddisk (c#)
I don't want the size of the partitions !!! but the "real size" of the disk. (hdd total size GB)

please help me..
Posted

See the responses to this question Finding All Drive Size From Hard disk [^]
 
Share this answer
 
There are a few articles about this here on Codeproject:

The best one I found is this (C++)
Get Physical HDD Serial Number without WMI[^]

Or this one using C#
How to Retrieve the REAL Hard Drive Serial Number[^]

This will give you a list of physical disks and some properties about each one.


And heres a simple script using WMI:

C#
var searcher = new ManagementObjectSearcher(@"select * from Win32_DiskDrive");
foreach (ManagementObject obj in searcher.Get())
{
    Console.WriteLine("Name:{0}", obj["Name"]);
    Console.WriteLine("Size:{0}", obj["Size"]);
}
 
Share this answer
 
v3
Hi,

try with DriveInfo Class.
DriveInfo (Properties)[^]
DriveInfo.TotalSize[^]

try like below.
C#
System.IO.DriveInfo info = new System.IO.DriveInfo();
Console.WriteLine(info.TotalSize); // gets the total size of the drive.


hope it helps.
 
Share this answer
 
Comments
StianSandberg 22-Apr-13 5:06am    
DriveInfo will give you all logical drives, not physical.
Karthik Harve 22-Apr-13 5:15am    
"Gets the total size of storage space on a drive" this is a definition given by MSDN. also, in the article, MSDN has provided an example which will calculate the spaces of the available drives. i guess, OP wanted the same.
StianSandberg 22-Apr-13 5:53am    
He writes "I don't want the size of the partitions". DriveInfo gives you the size of logical drives. If you want physical disks, you have to use WMI
essr42 22-Apr-13 5:22am    
I want to show the label1.text . How can ?
Karthik Harve 22-Apr-13 5:26am    
replace Console.WriteLine with lable1.Text.
label1.Text = info.TotalSize.ToString();
if you are following the example given in MSDN article, then sum up all the values and show the total value in a label.
You need to use the DriveInfo Class. Here is the MSDN DriveInfo Class link[^].

An example code:
C#
using System;
using System.IO;

class Info {
    public static void Main() {
        DriveInfo[] drives = DriveInfo.GetDrives();
        foreach (DriveInfo drive in drives) {
            Console.WriteLine(drive.Name);
            if (drive.IsReady) Console.WriteLine(drive.TotalSize);
        }
    }
}


Good luck,
OI
 
Share this answer
 
Comments
StianSandberg 22-Apr-13 5:09am    
DriveInfo will give you all logical drives, not physical.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900