• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • Last Modified:

Disk Cluster Size

Simple: How do I get the disk cluster size of a partition using WMI (Windows Management Instrumentation)? This is since I'm developing in C# and can't use DeviceIoControl.
0
Owlguru
Asked:
Owlguru
  • 8
  • 5
1 Solution
 
cookreCommented:
Specifics on default cluster sizes are listed here:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q140365


Short of using DeviceIoControl() or reading the MBR off the disk, the only way I can think of to determine cluster size is by experimentation, to wit, get available space, create a 1k file, check available space again, delete the 1k file, then create a 2k file and check available space again.  

Once you get a change in avaiable space you know you found the cluster size.

Another possibility would be to shell out to a chkdsk and parse the output.  Alas, I can't tell you if the format of the output varies among the various flavors of Windows.

Under 2k, the target line looks like:

x bytes in each allocation unit

0
 
OwlguruAuthor Commented:
cookre, I never thought of doing it by experimentation. I'm gonna try to try that :-). But what do I do for CD-ROMs and read-only disks?
0
 
cookreCommented:
Chances are, the only non-default cluster sizes you'll ever find areon hard drives on custom built boxes.  Note that includes custom configured commercial boxes.

Experimentation can also be done without writing files.  Count the number of files in various size categories (0-512, 513-1k, 1k+1-2k, 2k+1-4k,...), posit a cluster size, then see if the results jibe with reported space available.  If they don't, try a different size.  Unless a device has a weird cluster size, you'll hit on the right one eventually.

Specifically, cou

My preference would be chkdsk, assuming it's available and gives cluster size under your target OS's.  Any variations in OS specific output formatting can be dealt with easily.  Granted, though, it's not a very sexy solution.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
cookreCommented:
Ignore the 'Specifically, cou'.

Damned bifocals.
0
 
OwlguruAuthor Commented:
Isn't CHKDSK kinda slow? (It takes a while to finish checking the disk) Also, on Windows 2000 or XP, CHKDSK didn't display the cluster size. I like the experimentation method, so I'm gonna use that. I haven't had time to test it yet.

Thanks for everything
0
 
cookreCommented:
Yeah, it's slow.  BTW, I have it on 2k and xp.

Time for a duh moment.  Wouldn't just creating a 1 byte file reduce the avaiable space by one allocation unit? Duh.
0
 
OwlguruAuthor Commented:
Well, I tried that. Doesn't work. The free space stays the same, causing the app to report a 0 KB cluster size.
0
 
OwlguruAuthor Commented:
I think it might be because of WMI not updating the information, but even in drive properties, it doesn't change the free space. Weird.. I think the file is simply stored in the MFT (on the NTFS volume), because it's small enough. I heard somewhere, files under 512 bytes can be stored in the MFT.
0
 
OwlguruAuthor Commented:
Never mind it works now. I created a 256.1 KB file, then subtracted the new free space from the old free space and then subtracted a further 256 KB. Is that OK or will I run into trouble on drives with 256 KB cluster sizes? (though I doubt there are any)
0
 
OwlguruAuthor Commented:
Never mind it works now. I created a 256.1 KB file, then subtracted the new free space from the old free space and then subtracted a further 256 KB. Is that OK or will I run into trouble on drives with 256 KB cluster sizes? (though I doubt there are any)
0
 
OwlguruAuthor Commented:
sorry.. seems refreshing the browser posts a comment again :s
0
 
OwlguruAuthor Commented:
anyway I'm gonna grant you the 300 points since you gave me the idea of experimenting.
0
 
cookreCommented:
Well, if you come across an NT4.0 box with an 8-16gig drive with a 16-bit FAT and sector size above 512, you'll get a 256k cluster.

Sounds like a safe bet.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now