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 firemonkey应用程序中显示时隐藏虚拟键盘

Delphi idhttp最简洁的修改和取得cookie例子

Delphi的goto语句

Delphi测试数据库连接时间

Delphi程序在每个windows 会话中只执行一次

Delphi的串口通讯,数据接收实时性太差

Delphi 10.3 断点调试相关快捷键

Delphi webservices传文件

Delphi 将tbitmap与tgpimage转换

Delphi sccoloredid,星际争霸彩色 id 修改器 v0.2.0,支持 windows vista

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



打赏

取消

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

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

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

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

评论

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