如何使用dupError处理StringList中的重复项

Bil*_*ill -2 delphi delphi-xe4

这是尝试将文件名添加到字符串列表.如果向列表中添加重复的文件名,则引发异常并从列表中删除原始文件.我从帮助文件中了解到,如果找到重复文件,将返回现有条目的索引,然后可以使用该索引删除现有条目.

此代码的问题是,当找到重复时,不执行EListError异常中的代码.

我的问题是,如果存在重复的文件名,你如何从列表中删除原始文件名?实质上,如果在添加副本时检测到重复,我想从列表中删除原始文件.不幸的是,当将重复文件添加到列表时,异常陷阱中的代码不会执行.

{ Create a list of files to delete }
iListOfImagesToDelete := TStringList.Create;
try
  { Get a ListOfFiles in each collection }
  iCollectionList := TStringList.Create;
  try
    iListOfImagesToDelete.Duplicates := dupError;
    iListOfImagesToDelete.Sorted := True;
    { Add filenames to a stringlist with Duplicates set to dupError and Sorted set to True }
    iFileCount := iCollectionList.Count;
    for j := 0 to iFileCount - 1 do
      begin
        iFilename := iCollectionList[j];
        if FileExists(iFilename) then
        begin
           try
             iFileIndex := iListOfImagesToDelete.Add(iFilename);
           except
           { file exists in the list }
           on EListError do
           begin
              ShowMessage(ExtractFilename(ifilename) + ' exists.');
              { Remove the original duplicate file from the list }
              iListOfImagesToDelete.Delete(iFileIndex);
           end;
         end;
      end;
    end;
  finally
     iCollectionList.Free;
  end;
finally
    iListOfImagesToDelete.Free;
  end;
Run Code Online (Sandbox Code Playgroud)

Uwe*_*abe 5

提出的错误是EStringListError.你在找EListError.