Delphi – Checking File is Open in Delphi


Is there a way to check if a file has been opened by ReWrite in Delphi?

Code would go something like this:

AssignFile(textfile, 'somefile.txt');
if not textFile.IsOpen then

Best Answer

You can get the filemode. (One moment, I'll create an example).

TTextRec(txt).Mode gives you the mode:

55216 = closed
55217 = open read
55218 = open write

fmClosed = $D7B0;
fmInput  = $D7B1;
fmOutput = $D7B2;
fmInOut  = $D7B3;

Search TTextRec in the system unit for more information.