【问题标题】:How to list all Dir files in a ListBox in Android如何在Android的ListBox中列出所有Dir文件
【发布时间】:2014-12-20 12:37:05
【问题描述】:

如何列出 ListBox 中的所有 Dir 文件? 我在 Windows 中尝试了这段代码,它可以工作,但它在 Android 中不起作用。

procedure ListFileDir(Path: string; FileList: TStrings);
 var
  SR: TSearchRec;
    begin
       if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
      begin
      repeat
     if (SR.Attr <> faDirectory) then
  begin
    FileList.Add(SR.Name);
     end;
    until FindNext(SR) <> 0;
   FindClose(SR);
 end;
end;

  procedure TForm1.Button1Click(Sender: TObject);
begin
   ListFileDir('sdcard/1/', ListBox1.Items);
 end;

【问题讨论】:

    标签: android list delphi get


    【解决方案1】:

    您的代码仅适用于 Windows。对于跨平台开发,您应该在处理文件和文件夹时使用System.IOUtils

    具体来说,TDirectory.GetFiles(Path)

    uses
      System.Types,
      System.IOUtils;
    
    procedure ListFileDir(Path: string; FileList: TStrings);
    var
      Files: TStringDynArray;
      s: string;
    begin
      FileList.Clear;
      Files := TDirectory.GetFiles(Path);
      for s in Files do
        FileList.Items.Add(s);
    end;
    

    【讨论】:

    • 你能把代码传给我吗,这是我第一次做跨平台编程
    • 您应该填写一个本地字符串列表并将其分配给 FileList 或在修改前后使用FileList.BeginUpdateFileList.EndUpdate
    【解决方案2】:

    您的代码用于跨平台目的的问题不是您使用 FindFirst 和朋友本身(TDirectory.GetFiles 只是对它们的一个薄包装),而是 '*.*' 构造 - 您只需要使用 @ 987654324@改为:

    procedure ListFileDir(Path: string; FileList: TStrings);
    const
      AllFilesMask = {$IFDEF MSWINDOWS}'*.*'{$ELSE}'*'{$ENDIF};
    var
      SR: TSearchRec;
    begin
      if FindFirst(Path + AllFilesMask, faAnyFile, SR) = 0 then
      try
        repeat
          if (SR.Attr <> faDirectory) then
          begin
            FileList.Add(SR.Name);
          end;
        until FindNext(SR) <> 0;
      finally
        FindClose(SR);
      end;
    end;
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2020-12-28
      • 2017-01-20
      • 2021-08-12
      • 2021-01-23
      • 2021-04-14
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多