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

标签 delphi delphi-7 text-files

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

相关文章:

delphi - 尝试打开另一个表单时发生访问冲突

delphi - 如何重复播放一首歌?

python - 解析、查找章节并作为单独的文件写出

delphi - Delphi 中检测磁盘事件

当我尝试将 TClientDataSet.Active 设置为 true 时,Delphi 挂起

delphi - (delphi 应用程序)发现错误的位置

德尔福7 : Handling events in console application (TidIRC)

delphi - 如何使用 Delphi 7 进行命令行构建?

python - 拒绝读取Python文件的部分内容

c# - 从 C# 中的文本文件中读取矩阵