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
begin
try
ForceDirectories(ExtractFilePath(Value));
except
ErrorMessage(Value);
Exit;
end;
end;
if Value <> FLogFilename then
begin
Created := False;
if Assigned(FStream) then
FreeandNil(FStream);
if not FileExists(Value) then
begin
try
FStream := TFileStream.Create(Value,fmCreate);
Created := True;
finally
FreeAndNil(FStream);
end;
if not Created then
begin
ErrorMessage(Value);
Exit;
end;
FLogFilename := Value;
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
FLogFilename := Value;
FStream := TFileStream.Create(FLogFilename,fmOpenWrite or fmShareDenyWrite);
end;
end;
end;
If anyone has any advice, this will be appreciated.