gpt4 book ai didi

Delphi - 打开文本文件共享冲突

转载 作者:行者123 更新时间:2023-12-03 14:56:17 27 4
gpt4 key购买 nike

我尝试在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com