我想得到某个文件夹下的所有文件夹的名字,以下是我的代码,但有两个问题:
1只能在2000以上操作系统中能行,在98下就得不到;
2连该目录下的文件名也都加进去了,我只想要文件夹名。
望高手指教!
我的代码如下:
procedure TFSetting.GetAllNames;
var
i:integer;
vSearchRec: TSearchRec;
path:string;
begin
combobox_name.Items.Clear;
ComboBox_name.Items.Add('Null');
path:=ExtractFilePath(application.ExeName)+'Skins\*.*';
i := FindFirst(path, faDirectory, vSearchRec);
while i = 0 do begin
if (vSearchRec.Name<>'.') and (vSearchRec.Name<> '..') then
begin
ComboBox_name.Items.Add(vSearchRec.Name);
end;
i:= FindNext(vSearchRec);
end;
FindClose(vSearchRec);
end;
1只能在2000以上操作系统中能行,在98下就得不到;
2连该目录下的文件名也都加进去了,我只想要文件夹名。
望高手指教!
我的代码如下:
procedure TFSetting.GetAllNames;
var
i:integer;
vSearchRec: TSearchRec;
path:string;
begin
combobox_name.Items.Clear;
ComboBox_name.Items.Add('Null');
path:=ExtractFilePath(application.ExeName)+'Skins\*.*';
i := FindFirst(path, faDirectory, vSearchRec);
while i = 0 do begin
if (vSearchRec.Name<>'.') and (vSearchRec.Name<> '..') then
begin
ComboBox_name.Items.Add(vSearchRec.Name);
end;
i:= FindNext(vSearchRec);
end;
FindClose(vSearchRec);
end;
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;
递归
to:others,感谢各位的热心帮助,问题我已找到,是我测试时犯了个小错误,这段代码完全可以用。谢谢! 结贴!