首页 > 解决方案 > 如何释放这些 .txt 文件的内存

问题描述

使用 TXT 文件我最近看到一条消息“无法创建文件 ('c:\01.txt') 进程无法访问该文件,因为它正被另一个进程使用”我如何释放这些 .txt 的内存文件?

我有尼斯先生的这段代码,当我尝试更改或添加新代码时:ListBox1.Items.savetofile('01.txt');

“无法创建文件('c:\01.txt')进程无法访问该文件,因为它正被另一个进程使用”

var
  path: string;
  SR: TSearchRec;
  tempFile: TextFile;
  line: string;
begin
  path:= 'C:\my all txt\';
  if FindFirst(path + '*.txt', faAnyFile, SR) = 0 then
  begin
    repeat
      if (SR.Attr <> faDirectory) then
      begin
        AssignFile(tempFile, path + SR.Name);
        Reset(tempFile);
        while not Eof(tempFile) do
        begin
          Readln(tempFile, line);
          ListBox1.Items.Add(line);
        end;
      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;

标签: windowsdelphi-7

解决方案


完成文件后,您永远不会关闭它们。CloseFile到达后您需要使用Eof(tempfile)

while not Eof(tempfile) do
begin
  Readln(tempfile, line);
  ListBox1.Items.Add(line);
end;
CloseFile(tempfile);

推荐阅读