Delphi coding hints and tips.
Floppy in drive function
Here a little function that checks to see if a drive has some media in it, e.g. a floppy drive has a floppy disk in it, or a CD ROM drive has a disk in it. Thus before trying to save or load a file you can check the drive and present the user with a meaningful error message rather than just getting a IO error.
function MediaInDrive(const Drive: string): Boolean;
{works with WinNT and Win95}
{NOTE: Drive must in the from 'A:', this is so
can use function ExtractFileDrive for a file name.}
var
OldErrorMode: Cardinal;
SearchRec: TSearchRec;
RetVal: Integer;
begin
{set OS error mode so system does not display the
critical-error-handler message box when such an
error occurs. Instead, the operating system sends
the error to the calling process.}
OldErrorMode := SetErrorMode( SEM_FAILCRITICALERRORS );
try
{swicth off IO checking}
{$I-}
RetVal := FindFirst( Drive + '\*.*', faAnyfile, SearchRec );
FindClose( SearchRec );
{switch on IO checking}
{$I+}
case Retval of
0: Result := True; {disk with files}
-18: Result := True; {disk with no files}
else
Result := False; {no disk}
end;
finally
{Reset OS error mode}
SetErrorMode( OldErrorMode );
end;
end;


