Mega Code Archive

 
Categories / Delphi / Hardware
 

Get floppy disk type

Title: Get floppy disk type Question: Ever wanted to get the size and media type of a floppy disk? Answer: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); private Function GetDiskInfo(drive : Char) : Boolean; public { Public declarations } end; _DISK_GEOMETRY = Record Cylinders : int64; MEDIA_TYPE : longword; TracksPerCylinder : longword; SectorsPerTrack : longword; BytesPerSector : longword; End; Const IOCTL_DISK_GET_DRIVE_GEOMETRY = $0070000; MediaType : Array[0..12] of String = ('Unknown', '5.25" 1.2MB 512bytes/sector', '3.5" 1.44MB 512bytes/sector', '3.5" 2.88MB 512bytes/sector', '3.5" 20MB 512bytes/sector', '3.5" 720KB 512bytes/sector', '5.25" 360KB 512bytes/sector', '5.25" 320KB 512bytes/sector', '5.25" 320KB 1024bytes/sector', '5.25" 180KB 512bytes/sector', '5.25" 160KB 512bytes/sector', 'Removeable', 'Fixed Media'); var Form1 : TForm1; DISK_GEOMETRY : _DISK_GEOMETRY; implementation {$R *.dfm} Function TForm1.GetDiskInfo(drive : Char) : Boolean; Var Inf : THandle; Junk: dword; begin InF := CreateFile(PChar('\\.\'+drive+':'),GENERIC_READ,FILE_SHARE_READ, nil,OPEN_EXISTING,0,0); if (InF = INVALID_HANDLE_VALUE) Then Begin Result := False; Exit; End; Result := DeviceIoControl(InF, IOCTL_DISK_GET_DRIVE_GEOMETRY, Nil, 0, @DISK_GEOMETRY, sizeof(_DISK_GEOMETRY), Junk, nil); CloseHandle(Inf); end; procedure TForm1.Button1Click(Sender: TObject); Begin If (GetDiskInfo('A')) Then Begin memo1.Lines.Append('Media Type: ' + MediaType[DISK_GEOMETRY.MEDIA_TYPE]); memo1.Lines.Append('Cylinders: ' + inttostr(DISK_GEOMETRY.Cylinders)); memo1.Lines.Append('Tracks Per Cylinder: ' + inttostr(DISK_GEOMETRY.TracksPerCylinder)); memo1.Lines.Append('Sectors Per Track: ' + inttostr(DISK_GEOMETRY.SectorsPerTrack)); memo1.Lines.Append('Bytes Per Sector: ' + inttostr(DISK_GEOMETRY.BytesPerSector)); End Else memo1.Lines.Append('Error retrieving disk information.'); End; end. {------------------------------- Total size of disk (in bytes) = BytesPerSector * SectorsPerTrack * TracksPerCylinder * Cylinders; --------------------------------}