Delphi xe IOUtils 单元(1): 初识 TDirectory.GetFiles


本文整理自网络,侵删。

 
用 IOUtils 单元下的 TDirectory.GetFiles 获取文件列表太方便了;

下面的例子只是 TDirectory.GetFiles 的典型应用...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses IOUtils, Types;

{这是随意拿来的测试路径}
const path = 'C:\Program Files\Embarcadero\RAD Studio\7.0';

//获取指定目录下的所有文件
procedure TForm1.Button1Click(Sender: TObject);
var
  dir: TDirectory; {这是个结构, 后面就不再声明了, 将直接使用}
  files: TStringDynArray; {TStringDynArray = array of string;}
  str: string;
begin
  files := dir.GetFiles(path);
  Memo1.Clear;
  for str in files do Memo1.Lines.Add(str);
end;

//获取指定目录下的所有指定类型的文件
procedure TForm1.Button2Click(Sender: TObject);
var
  files: TStringDynArray;
  str: string;
begin
  files := TDirectory.GetFiles(path, '*.txt');
  Memo1.Clear;
  for str in files do Memo1.Lines.Add(str);
end;

//获取指定目录及其嵌套目录下的所有指定类型的文件
procedure TForm1.Button3Click(Sender: TObject);
var
  files: TStringDynArray;
  str: string;
begin
  files := TDirectory.GetFiles(path, '*.txt', TSearchOption.soAllDirectories);
  Memo1.Clear;
  for str in files do Memo1.Lines.Add(str);
end;

end.

相关阅读 >>

Delphi 遍历硬盘所有文件目录

Delphi 获取计算机名和用户名

Delphi 10 seattle的android应用程序中使用参数启动服务

Delphi fdconnection1.gettablenames 查看所有表包含用户表和系统表

Delphi 每年、月、周、日的开始与结束的时间

Delphi idhttp控件断点下载

Delphi 判断尾串是否匹配,不分大小写

Delphi for 循环的例子 多种用法

Delphi代码直接注入别的进程

Delphi 10.4 freeandnil 问题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...