Description |
The IOResult function retrieves the result of the last I/O (file input/output) operation.
This number is zero if the operation succeeded, or a positive number if it failed.
Use IOResult when you have disabled the default Delphi IO error trapping. When $IOChecks is On, Delphi raises exceptions for the errors. When Off, Delphi does not raise exceptions, requiring the code to inspect IOResult.
|
|
Notes |
Warning : retrieval of the IO result is a one-off activity - the retrieval resets the value to 0.
|
|
Related commands |
$IOChecks |
|
When on, an IO operation error throws an exception |
GetLastError |
|
Gives the error code of the last failing Windows API call |
|
|
|
Example code : Create a directory twice, catching the error code |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses // The System unit does not need to be defined SysUtils, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); var
error : Integer;
begin // Try to create a new subdirectory in the current directory // Switch off I/O error checking
{$IOChecks off}
MkDir('TempDirectory');
// Did the directory get created OK?
error := IOResult;
if error = 0
then ShowMessage('Directory created OK')
else ShowMessageFmt('Directory creation failed with error %d',[error]);
// Try to create the directory again - this will fail!
MkDir('TempDirectory'); error := IOResult; // Save the return code
if error = 0
then ShowMessage('Directory created OK again')
else ShowMessageFmt('Repeat creation failed with error %d',[error]);
// Delete the directory to tidy up
RmDir('TempDirectory');
// Switch IO checking back on
{$IOChecks on}
end; end.
|
Hide full unit code |
Directory created OK
Repeat creation failed with error 183
|
|