delphi中如何生成一个目录(包括子目录)的文件列表

来源:百度知道 编辑:UC知道 时间:2024/05/31 05:25:43
使用delphi如何生成一个目录(包括子目录)的文件列表呢,就象一些播放器那样,指定一个目录,就可以把目录及子目录下所有的音乐文件添加到列表里.

function FileList(Path: string; Attr: Integer = FAAnyfile): TStrings;
var
SR: TSearchRec;
begin
Result := TStringList.Create;
if Path[Length(Path)] <> '\' then
Path := Path + '\';
if (Attr and FADirectory) <> FADirectory then
Attr := Attr or FADirectory;
if FindFirst(Path + '*.*', Attr, SR) = 0 then
repeat
if (SR.Attr and FADirectory) = FADirectory then begin
if (SR.Name <> '.') and (SR.Name <> '..') then
Result.AddStrings(FileList(Path + SR.Name));
end
else
Result.Add(Path + SR.Name);
until FindNext(SR) <> 0;
FindClose(SR);
end;

利用递归遍历该目录下的所有子目录文件,
我有原代码,可以加我QQ78513746
利用findfirst,findnext,这样自由空间更大

在控件栏打开 win31 选项,使用文件列表控件:filelistbox,directorylistbox,drivecombobox即可轻松实现