-1

您好,我正在尝试将文件夹中的文件名列表加载到 Stringlist 中。我只想添加以 .jpg 结尾的文件的名称。当我现在运行它时,会出现访问冲突错误。这是我得到的。

选择目录。

procedure TMainForm.Load1Click(Sender: TObject);
var
  DirName: string;
begin
    mylist.Free;
    myList := TList<TBitmap>.Create;
  DirName := 'c:\';
  if SelectDirectory(DirName,[sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP)
  then;
    LoadImages(DirName);
end;

获取文件夹中的总文件数

function GetFilesCount(Dir : string; Mask : string) : integer;
var
  Path : string;
begin
  Result := 0;
  for Path in TDirectory.GetFiles(Dir, Mask) do
    inc(Result);
end;

从文件夹中获取文件名

function TMainForm.GetFilenames(Path: string ):TStrings;
var
  Dest: TStrings;
  SR: TSearchRec;
begin
  Dest.create;
  if FindFirst(Path+'*.*', faAnyFile, SR) = 0 then
  repeat
    Dest.Add(SR.Name);
  until FindNext(SR) <> 0;
  FindClose(SR);
  Result := Dest;
  Dest.Free;
end;

和负载图

    procedure TMainForm.LoadImages(const Dir: string);
    const
      FIRST_IMAGE = 0;
    var
      iFile : Integer;
      CurFileName: string;
      FoundFile : boolean;
      FileNameTemplate : string;
      FileNames : Tstrings;
    begin
       FileNames.Create;
       FileNameTemplate := IncludeTrailingPathDelimiter(Dir) + '*.jpg';          FileNames:=GetFileNames(FileNameTemplate);                                                 
      try
        ifile := 0;                                                                 
        repeat
          CurFileName := FileNames.Names[ifile];                                      
          showmessage(Curfilename);
 if FoundFile then
      begin
        end;
        Inc(iFile);
      end;
    until not FoundFile;
end;
4

2 回答 2

5

问题就在这里(实际上您有两个问题) - 实际上,您的代码中有两次相同的问题:

Dest.Create;  // In GetFileNames

FileNames.Create; // In LoadImages

首先,TStrings是一个抽象类。您不能创建它的实例;它是其他更具体的类的基础,例如TStringList. 改为创建这些具体类之一。

其次,您必须创建TStringList并存储对它的引用。

FileNames := TStringList.Create;

更好的方法是创建您的字符串列表并将其传递给函数或过程,因为您需要它返回时的结果。我将为您快速浏览其中一个:

function TMainForm.GetFilenames(const Path: string; const FileList: TStringList ): Boolean;
var
  SR: TSearchRec;
begin
  Assert(Assigned(FileList));      // Make sure it was created and passed in
  FileList.Clear;                  // Remove any previous names
  if FindFirst(Path+'*.*', faAnyFile, SR) = 0 then
  repeat
    FileList.Add(SR.Name);
  until FindNext(SR) <> 0;
  FindClose(SR);
  Result := FileList.Count > 0;  // Return true if we have found any files
end;

像这样称呼它:

FileNames := TStringList.Create;
try
  if GetFileNames(PathToFolder, FileNames) then
  begin
    // Process files here
    for i := 0 to FileNames.Count - 1 do
    begin
      CurrFile := FileNames[i];
      // Use the file here from CurrFile
    end;

  end;
finally
  FileNames.Free;
end;
于 2012-06-18T18:57:10.423 回答
0
function GetAllFiles(AFolder: string; AMask: string; AStringList: TStringList): integer;
// Add all files matching AMask in AFolder to AStringList. Returns number of files
var
  iSearch: TSearchRec;
  iCount: integer;
  iFileAttrs: integer;
begin
  iCount := 0;
  iFileAttrs := $23 - faHidden;
  // Find all files
  if FindFirst(IncludeTrailingPathDelimiter(AFolder) + AMask, iFileAttrs, iSearch) = 0 then
  begin
    repeat
      // Add the files to the stringlist
      AStringList.Add(IncludeTrailingPathDelimiter(AFolder) + iSearch.name);
      inc(iCount);
    until FindNext(iSearch) <> 0;
  end;
  FindClose(iSearch);
  Result := iCount;
end;
于 2012-06-18T18:58:44.470 回答