Title: File is locked?
Question: Ever wrote an application which takes it's input from a file generated by another application? The problem here is that you need to wait until the other app has finished writing to the file before you open it.
Answer:
I couldn't find an WIN32 API function which does this so I wrote my own:
function E9FileStatus(Const Origin: string): boolean;
var
F: TFileStream;
begin
{
Value Meaning
fmCreate Create a file with the given name. If a file with the given name exists, open the file in write mode.
fmOpenRead Open the file for reading only.
fmOpenWrite Open the file for writing only. Writing to the file completely replaces the current contents.
fmOpenReadWrite Open the file to modify the current contents rather than replace them.
The share mode must be one of the following values:
Value Meaning
fmShareCompat Sharing is compatible with the way FCBs are opened.
fmShareExclusive Other applications can not open the file for any reason.
fmShareDenyWrite Other applications can open the file for reading but not for writing.
fmShareDenyRead Other applications can open the file for writing but not for reading.
fmShareDenyNone No attempt is made to prevent other applications from reading from or writing to the file.
If the file can not be opened, Create will raise an exception.
Return true if the file is not locked
}
try
F := TFileStream.Create(Origin, fmOpenReadWrite OR fmShareExclusive);
try
Result := true;
finally
F.Free;
end;
except
Result := false;
end;
end;