我尝试在 Delphi 7 应用程序中打开一个文本文件进行读取,但收到 I/O 错误 32(共享冲突),因为另一个应用程序已打开该文件。我尝试将 FileMode 设置为“fmOpenRead 或 fmShareDenyNone”,但现在意识到这并不适用于文本文件。
有没有办法读取其他应用程序打开的文本文件?
var
f: TextFile;
begin
FileMode := fmOpenRead or fmShareDenyNone; // FileMode IS NOT APPLICABLE TO TEXT FILES!!
AssignFile(f, FileName);
Reset(f);
最佳答案
使用TStringList的LoadFromStream
方法,而不是LoadFromFile
。您可以通过这种方式控制锁定:
var
slFile: TStrings;
stream: TStream;
begin
slFile := TStringList.Create;
try
stream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);
try
slFile.LoadFromStream(stream);
finally
stream.Free;
end;
//Use the stringlist
finally
slFile.Free;
end;
end;
该示例使用流加载到 TStringList
中。如果你只想阅读文章,你可以这样做。只需从流中读取。
关于Delphi - 打开文本文件共享冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/790655/