Get Hard Disk Size dynamically in Cocoa
I'm trying to get the the size of the main HD. Here's the code I'm using:
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"/" error:nil];
But this code doesn't give me the correct size of the HD. If my HD 开发者_高级运维has a capacity of 320gb that will return 290GB. I think there are other volumes but I don't know how to calculate them.
What I want to know is a way to get the total size of my hard drive.
It sounds like you're getting the correct answer, actually. People who sell hard drives like to use the power-of-10 gigabyte to describe the capacity of the disk, while the computer itself cares about how many multiples of 2 are available, and displays the gibibytes. A gibibyte is bigger than a gigabyte. The SI prefix "giga-" means 109; the so-called binary prefix "gibi-" was chosen to be the closest power of two, 230, which is 1,073,741,824.
The true number of bytes is the same in either case, it's just reported differently depending on whether you divide by 1,000,000,000 or 1,073,741,824. For example, 320,000,000,000 ÷ 1,073,741,824 = 298.023, and you might lose a bit more to area the disk reserves for itself.
精彩评论