Delphi7.0中如何删除文件夹(带子目录和文件)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Delphi7.0中如何删除文件夹(带子目录和文件)
说明:先要在USES中输入:ShellApi;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, V ariants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons,ShellApi;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DeletePath(mDirName: string): Boolean; { 返回删除指定目录是否成功}
var
vSearchRec: TSearchRec;
vPathName: string;
K: Integer;
begin
Result := True;
vPathName := mDirName + '\*.*';
K := FindFirst(vPathName, faAnyFile, vSearchRec);
while K = 0 do begin
if (vSearchRec.Attr and faDirectory > 0) and
(Pos(, '..') = 0) then begin
FileSetAttr(mDirName + '\' + , faDirectory);
Result := DeletePath(mDirName + '\' + );
end else if Pos(, '..') = 0 then begin
FileSetAttr(mDirName + '\' + , 0);
Result := DeleteFile(PChar(mDirName + '\' + ));
end;
if not Result then Break;
K := FindNext(vSearchRec);
end;
FindClose(vSearchRec);
Result := RemoveDir(mDirName);
end; { DeletePath }
procedure TForm1.BitBtn1Click(Sender: TObject);{直接调用函数就可以删除,放到前面} begin
if DeletePath('c:\liao') then
showmessage('Y es')
else
showmessage('no');
end;
end.