使用SHFileOperation函数
function TForm1.DeleteDir:integer;
var
source:string;
abortbyuser:boolean;
ShFileOpStruct: TShFileOpStruct;
begin
source:='c:\dirname\0\0';
fillchar(ShFileOpStruct, sizeof(ShFileOpStruct), 0); //变量清零
with ShFileOpStruct do
begin
Wnd:=Application.MainForm.Handle; //给窗口句柄赋值
wFunc:=FO_DELETE; //实现文件删除
pFrom:=PChar(Source); //source为源文件名
fFlags:=FOF_ALLOWUNDO ;
Result := ShFileOperation(ShFileOpStruct); //执行命令
AbortByUser:=fAnyOperationsAborted;
end;
end;
function TForm1.DeleteDir:integer;
var
source:string;
abortbyuser:boolean;
ShFileOpStruct: TShFileOpStruct;
begin
source:='c:\dirname\0\0';
fillchar(ShFileOpStruct, sizeof(ShFileOpStruct), 0); //变量清零
with ShFileOpStruct do
begin
Wnd:=Application.MainForm.Handle; //给窗口句柄赋值
wFunc:=FO_DELETE; //实现文件删除
pFrom:=PChar(Source); //source为源文件名
fFlags:=FOF_ALLOWUNDO ;
Result := ShFileOperation(ShFileOpStruct); //执行命令
AbortByUser:=fAnyOperationsAborted;
end;
end;
function DeleteFile(const FileName: string): Boolean;
function RemoveDir(const Dir: string): Boolean;
procedure EraseTree(DirName:string);
var
DirInfo: TSearchRec;
DosError: Integer;
begin
DosError := FindFirst(DirName+'\*.*', FaAnyfile, DirInfo);
while DosError = 0 do
begin
if ((DirInfo.Attr and FaDirectory)= faDirectory) and (DirInfo.Name<>'.') and (DirInfo.Name<>'..')
then EraseTree(DirName + '\' + DirInfo.Name);
if ((DirInfo.Attr and FaDirectory <> FaDirectory) and (DirInfo.Attr and FaVolumeId <> FaVolumeID))
then DeleteFile(pChar(DirName+'\' + DirInfo.Name));
DosError := FindNext(DirInfo);
end;
SysUtils.FindClose(DirInfo);
RemoveDirectory(pchar(DirName));
end;