Delphi Example code : Try to delete a file twice


SUBMITTED BY: kendiboy

DATE: Dec. 21, 2017, 4:38 p.m.

UPDATED: Dec. 21, 2017, 4:38 p.m.

FORMAT: Delphi

SIZE: 966 Bytes

HITS: 383

  1. Example code : Try to delete a file twice
  2. var
  3. fileName : string;
  4. myFile : TextFile;
  5. data : string;
  6. begin
  7. // Try to open a text file for writing to
  8. fileName := 'Test.txt';
  9. AssignFile(myFile, fileName);
  10. ReWrite(myFile);
  11. // Write to the file
  12. Write(myFile, 'Hello World');
  13. // Close the file
  14. CloseFile(myFile);
  15. // Reopen the file in read mode
  16. Reset(myFile);
  17. // Display the file contents
  18. while not Eof(myFile) do
  19. begin
  20. ReadLn(myFile, data);
  21. ShowMessage(data);
  22. end;
  23. // Close the file for the last time
  24. CloseFile(myFile);
  25. // Now delete the file
  26. if deletefile(fileName)
  27. then ShowMessage(fileName+' deleted OK')
  28. else ShowMessage(fileName+' not deleted');
  29. // Try to delete the file again
  30. if deletefile(fileName)
  31. then ShowMessage(fileName+' deleted OK again!')
  32. else ShowMessage(fileName+' not deleted, error = '+
  33. IntToStr(GetLastError));
  34. end;

comments powered by Disqus