function ListDirs(Path: string; List: TStringList): Integer;
var
FindData: TWin32FindData;
FindHandle: THandle;
FileName: string;
AddToList: Boolean;
begin
Result := 0;
AddToList := Assigned(List);
if Path[Length(Path)] <> '/' then
Path := Path + '/';
Path := Path + '*.*';
FindHandle := Windows.FindFirstFile(PChar(Path), FindData);
while FindHandle <> INVALID_HANDLE_VALUE do
begin
FileName := StrPas(FindData.cFileName);
if (FileName <> '.') and (FileName <> '..') and
((FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) then
begin
Inc(Result);
if AddToList then
List.Add(FileName);
end;
if not Windows.FindNextFile(FindHandle, FindData) then
FindHandle := INVALID_HANDLE_VALUE;
end;
Windows.FindClose(FindHandle);
end;
使用:
var
s: TStringList;
begin
s := TStringList.Create;
ListDirs('c:/windows/', s);
ListBox1.Items.AddStrings(s);
s.Free;
end;