本文整理自网络,侵删。
delphi删除目录及子目录及所有目录下的文件
uses shellapi;
Function DelTree(DirName : string): Boolean;
var
SHFileOpStruct : TSHFileOpStruct;
DirBuf : array [0..255] of char;
begin
try
Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0);
FillChar(DirBuf, Sizeof(DirBuf), 0 );
StrPCopy(DirBuf, DirName);
with SHFileOpStruct do begin
Wnd := 0;
pFrom := @DirBuf;
wFunc := FO_DELETE;
fFlags := FOF_ALLOWUNDO;
fFlags := fFlags or FOF_NOCONFIRMATION;
fFlags := fFlags or FOF_SILENT;
end;
Result := (SHFileOperation(SHFileOpStruct) = 0);
except
Result := False;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if DelTree('c:\abc') then
ShowMessage('Directory deleted!')
else
ShowMessage('Errors occured!');
end;
相关阅读 >>
Delphi 辨析 field、fielddef、fields、fielddefs、fieldlist、fielddeflist
Delphi setcursorpos函数在窗口上定位鼠标坐标
更多相关阅读请进入《Delphi》频道 >>