I can't seem to reliably determine if an existing text file is open in notepad.

I can't seem to reliably determine if an existing text file is open in notepad.

I've tried numerous methods including creating a TFileStream with fmOpenReadWrite or fmShareExclusive - it always creates a stream without conflict.

I've tried the "IsFileInUse" code that seems to be popular:

function IsFileInUse(const fName: TFileName): Boolean;
var
   HFileRes: HFILE;
begin
   Result := False;
   HFileRes := CreateFile(PChar(fName),
                          GENERIC_READ or GENERIC_WRITE,
                          0,
                          nil,
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          0);
   Result := (HFileRes = INVALID_HANDLE_VALUE);
   if not Result then
     CloseHandle(HFileRes);
end;

I've tried to see if the file is locked.

And I've tried to determine the file last accessed time to see if the time is within a certain period close to the current time. Never returns what is supposed to be even close to the last accessed time.

Any suggestions or ideas? I'm currently working with existing code on D5 before transitioning to Seattle.

Comments