Search code examples
delphifile-iotstringlist

Want to read a file to a TStringList


Yes I want to read a simple a logfile into a TStringList and that is easy done with LoadFromFile. But the problem is that the file can be already opened by another program at the same time so an exception may appear. I have tried to use:

FileMode := fmShareCompat;

But it won't work.

I have also tried to use:

fFilePath := fPathList[PathIndex] + '\' + FileData.Name;
AssignFile(vFile, fFilePath);
Reset(vFile, 1);  // Recordsize = 1

SetLength(vFileString, FileData.Size);
BlockRead(vFile, vFileString, FileData.Size);   
vCurrentFile.Text := vFileString;

It raise an EInOutError with message I/O error 998.

Any suggestion ?


Solution

  • Try LoadFromStream and do something like:

    fileStream := TFileStream.Create(aFileName, fmShareDenyNone);
    myTStringList.LoadFromStream(fileStream);
    fileStream.Free();