Reputation: 5166
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 ?
Upvotes: 2
Views: 4511
Reputation: 14286
Try LoadFromStream and do something like:
fileStream := TFileStream.Create(aFileName, fmShareDenyNone);
myTStringList.LoadFromStream(fileStream);
fileStream.Free();
Upvotes: 13
Reputation: 1791
Also, try..except and try..finally are good friends at these times. Encapsulate your file reading code in these types of blocks and tell the user about the problem that arises.
Upvotes: 1
Reputation: 38743
fmShareCompat should probably be marked as deprecated. You want fmShareDenyNone (as Drejc said)
fmShareCompat comes from 16 DOS days I believe. On Windows, it is treated the same as fmShareExclusive. When Linux was supported it was treated the same as fmShareDenyNone.
Upvotes: 4