Re: How can I tell if a file is "open"?

Posted by webmaster Guido on March 01, 2007

In Reply to How can I tell if a file is posted by Lee Hallin P12462 on February 27, 2007

I looked in the various HELP groups and did not find a function that would return the open/close status of a given file (File or Text). Is there such a function or procedure?

I have done this in the past with a global "FileIsOpen" boolean "switch" that I set in my open and close procedures. I was just wanting to do it cleaner :-)

There is no Delphi function that tells you if a file is "open".
I suppose that's because there's a lot of confusion about the term "open". Do you mean that a file is "locked" (by your own or another application), or your application has opened a file for reading, or your application has opened a file for writing?

Your global "FileIsOpen" boolean variable is the next-to-best solution. However, it's better to open and close the file in the *same* routine, with a try...finally or try...except construction, thus making sure that the file always is closed or unlocked at the end of the routine.

Related articles

File is "in use"
How can I tell if a file is being used by another program?
     

Follow Ups