Disk Information

vegaseat 2 Tallied Votes 149 Views Share

Get simple information like serial number, bytes/sector, sectors/cluster, free and total disk space of your hard drive using Windows API calls.

// get hard drive info for the C: drive
// original Delphi Pascal, recoded in C and tested with Pelles C
// vegaseat      27nov2004 
// (add two \\ to the end of C: if missing)

#include <stdio.h>     // printf()
#include <windows.h>   // windows api header

int main()
{
  DWORD VolumeSerialNumber = 0;
  DWORD MaximumComponentLength = 0;
  DWORD FileSystemFlags = 0;
  
  DWORD SectorsPerCluster = 0;
  DWORD BytesPerSector = 0;  
  DWORD FreeClusters = 0;  
  DWORD TotalClusters = 0;

  DWORD FreeSpace = 0;      // free space available
  DWORD TotalSpace = 0;     // total space available

  GetVolumeInformation("C:\\", NULL, 0, &VolumeSerialNumber, &MaximumComponentLength,
                        &FileSystemFlags, NULL, 0);
  printf("Serial number for drive C = %X\n\n",VolumeSerialNumber);
  
  GetDiskFreeSpace("C:\\", &SectorsPerCluster, &BytesPerSector, 
                   &FreeClusters, &TotalClusters);
                   
  FreeSpace  = BytesPerSector * SectorsPerCluster * FreeClusters;
  TotalSpace = BytesPerSector * SectorsPerCluster * TotalClusters;
  
  printf("Bytes/Sector    = %d\n",BytesPerSector);
  printf("Sectors/Cluster = %d\n\n",SectorsPerCluster);
  
  printf("Drive C Total Disk Space: %Ld bytes\n",TotalSpace);  
  printf("Drive C Free  Disk Space: %Ld bytes\n",FreeSpace);
 
  getchar();  // wait
  return 0;
}
sytheron 0 Newbie Poster

Thank you, I found what I've been looking for. (the c syntax)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.