当我在文件夹中有子文件夹时 – 此代码不是删除文件夹…是否有任何错误?
procedure TForm.Remove(Dir: String); var Result: TSearchRec; Found: Boolean; begin Found := False; if FindFirst(Dir + '\*',faAnyFile,Result) = 0 then while not Found do begin if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name) else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name); Found := FindNext(Result) <> 0; end; FindClose(Result); RemoveDir(Dir); end;
如果我是你,我只是告诉操作系统删除包含其所有内容的文件夹.通过编写(使用ShellAPI)
var ShOp: TSHFileOpStruct; begin ShOp.Wnd := Self.Handle; ShOp.wFunc := FO_DELETE; ShOp.pFrom := PChar('C:\Users\Andreas Rejbrand\Desktop\Test\'#0); ShOp.pTo := nil; ShOp.fFlags := FOF_NO_UI; SHFileOperation(ShOp);
[如果你这样做
ShOp.fFlags := 0;
相反,你得到一个很好的确认对话框.如果你这样做
ShOp.fFlags := FOF_NOCONFIRMATION;
你没有得到确认对话,但如果操作很长,你会得到一个进度条.最后,如果添加FOF_ALLOWUNDO标志,则将目录移动到Waste Bin而不是永久删除它.
ShOp.fFlags := FOF_ALLOWUNDO;
当然,您可以根据需要组合标志:
ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO;