Mega Code Archive

 
Categories / Delphi / Forms
 

Low Level Formatting of Drives

Title: Low Level Formatting of Drives Question: How do you do a kind of low level format of a drive? Answer: This article follows on from "Creating and restoring images of drives?, http://www.delphi3000.com/articles/article_2812.asp". I should clear up some miss-givings of CreateFile() in Disk Access - under Windows 9x (as far as I know), you can only use CreateFile() on non hard drive disks, though I do have a complex unit to get the job done under 9x if you want it - URL later. Right, now this low level format is really simple, I'm assuming floppy disk for now: procedure LowLevelFormat; var Disk: THandle; Buffer: array [1..512] of Byte; WroteOK: Boolean; BufWrote: DWORD; begin Disk := CreateFile('\\.\A:',GENERIC_WRITE,FILE_SHARE_READ,nil,OPEN_EXISTING,0,0); if Disk = 0 then begin ShowMessage('Error - Could not open device.'); Exit; end; FillChar(Buffer,SizeOf(Buffer),$00); WroteOK := True; BufWrote := 1; while (WroteOK = True) and (BufWrote 0) do begin WroteOK := WriteFile(Disk,Buffer,SizeOf(Buffer),BufWrote,nil); end; CloseHandle(Disk); end; This method null's every byte on disk, removing boot sector, FAT tables and all! Of course its fairly slow, you could try asynchranous null'ing of a number of sectors, or increasing the buffer size - experiment! The URL to the unit is: http://www.workshell.co.uk/dev/delphi/DejanMaksimovicAlfa32.zip