繁体   English   中英

如何在Android中的列表框中列出所有Dir文件

[英]How to list all Dir files in a ListBox in Android

如何列出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;

您的代码仅适用于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;

用于跨平台用途的代码的问题不是您使用FindFirst和类似的朋友( TDirectory.GetFiles只是它们的一个薄包装),而是使用'*.*'构造-您只需要使用'*'

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM