想要将文件读取到 TStringList
是的,我想将一个简单的日志文件读入 TStringList,这可以使用 LoadFromFile 轻松完成。 但问题是该文件可能已被另一个程序同时打开,因此可能会出现异常。 我尝试过使用:
FileMode := fmShareCompat;
但它不起作用。
我也尝试过使用:
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;
它引发 EInOutError 并显示消息 I/O 错误 998。
有什么建议吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 LoadFromStream 并执行以下操作:
Try LoadFromStream and do something like:
fmShareCompat 可能应该被标记为已弃用。 你想要fmShareDenyNone(正如Drejc所说)
我相信 fmShareCompat 来自 16 DOS 天。 在 Windows 上,它的处理方式与 fmShareExclusive 相同。 当支持 Linux 时,它的处理方式与 fmShareDenyNone 相同。
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.
另外,try.. except 和 try..finally 在这些时候都是好朋友。 将文件读取代码封装在这些类型的块中,并告诉用户出现的问题。
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.