Delphi writes network resource using TFilestream, locks file when network is lost

I am trying to write a network resource (local) using TFilestream. Everything works fine if the network connection is not confused.

However, if I pulled out the network cable and then reconnected to it, subsequent attempts to open the feedstream will fail because of access restrictions. I also can’t even delete the file in Explorer! TFilestream seems to be blocking the file, and the only way around this is to reboot.

In my application, I keep the file open all the time that I write to it (this is a log file that is written once per second).

My code that crashes is below:

procedure TFileLogger.SetLogFilename(const Value: String);
var line : String;
Created : Boolean;
begin
  if not DirectoryExists(ExtractFilePath(Value)) then //create the dir if it doesnt exist
  begin
       try
         ForceDirectories(ExtractFilePath(Value));
       except
         ErrorMessage(Value); //dont have access to the dir so flag an error
         Exit;
       end;
  end;
  if Value <> FLogFilename then //Either create or open existing
  begin
      Created := False;          
      if Assigned(FStream) then
         FreeandNil(FStream);
      if not FileExists(Value) then   //create the file and write header
      begin
           //now create a new file
           try
              FStream := TFileStream.Create(Value,fmCreate);
              Created := True;
           finally
             FreeAndNil(FStream);
           end;
           if not Created then //an issue with creating the file
           begin
                ErrorMessage(Value);
                Exit;
           end;
           FLogFilename := Value;
           //now open file for writing
           FStream := TFileStream.Create(FLogFilename,fmOpenWrite or fmShareDenyWrite);
           try
              line := FHeader + #13#10;
              FStream.Seek(0,soFromEnd);
              FStream.Write(Line[1], length(Line));
              FSuppress := False;
           except
              ErrorMessage(Value);  
           end;
      end else begin //just open it
           FLogFilename := Value;
           //now open file for writing
           FStream := TFileStream.Create(FLogFilename,fmOpenWrite or fmShareDenyWrite); //This line fails if the network is lost and then reconnected
      end;
  end;
end;

If anyone has any advice, this will be appreciated.

+5
2

, Network Share API, NetFileEnum NetFileClose. .

+7

- , TFileStream. SysUtils. , , :

// variables used in pseudo-code below
var
  fHandle, bytesWriten: Integer;
  Value: string;
  • fHandle := FileOpen('filename', fmOpenReadWrite or ...).
  • Verify is fHandle > -1, sleep loop, .
  • bytesWritten := FileWrite(fHandle, Value, Length(Value));.
  • bytesWritten, = Length(Value).
  • bytesWritten 0, , . try ... finally if fHandle > -1 then try FileClose(fHandle); except end;, , .
  • bytesWritten 0, .

, , , :

if fHandle > -1 then
  try
    FileClose(fHandle);
  except
  end;

() , , . , . - ...

0

All Articles