Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to verify SaveToFile method is successful?

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I'm trying to verify and error trap the saving of a StringList to a file using SaveToFile, to ensure it has completed successfully, or tell me if it can't.

I couldn't seem to do something like:
Code:
if not StringList1.SaveToFile('file.txt') then action

so I tried the routine here:

and called it with this:
Code:
if IsFileInUse('file.txt') then 
    begin
    PlaySound('SystemQuestion', 0, SND_ALIAS or SND_ASYNC);
    ShowMessage('Could not save to disk');
    end
  else
    StringList1.SaveToFile('file.txt');

I ran the code once to create the file file.txt, then opened the file in notepad and then notepad++, and ran the routine again.
The routine ran error free each time, no report of the file in use, and the routine (which adds text to the lines) was successful each time, despite my having it open elsewhere.

Am I doing something wrong, or is there a better way?


Steve (Delphi 2007 & XP)
 
The routine ran error free each time, no report of the file in use, and the routine (which adds text to the lines) was successful each time, despite my having it open elsewhere.

The question is *how* those programs have the file open. If they just read the file into memory without any exclusive locks (or remove the read locks when done), then any other program will get full ability to access the file as well.

Not coincidentally, this is what you are noticing, since most programs are not written to exclusively lock a file. Therefore, much concern of this is not usually necessary (999 times out of 1000, IsFileInUse will be false). Perhaps the only reasons I can think of for such a thing are space considerations and an environment where concurrency is an issue.

May I ask what concern is present to try to verify this problem?

 
well really, all I really want to do is know I have saved the file to disk without any errors.
Is there a function that returns true on success with SaveToFile?


Steve (Delphi 2007 & XP)
 
Is there a function that returns true on success with SaveToFile?

No. Exception EWriterError will happen when it does though. If you don't get any crash messages, then assume it to be a success.
 
OK, fair enough.
Thanks Glenn


Steve (Delphi 2007 & XP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top