1个回答
展开全部
TSearchRec是Delphi封装的Windows文件信息的一个结构体,用于遍历文件夹,文件,通常和FindFirst、FindNext配合使用。以下是采用递归方法删除文件夹的函数。
function DeleteDirectory(Directory: string): Integer;
var
SR: TSearchRec;
FR: Integer;
begin
Result:= -1;
if not DirectoryExists(Directory) then
Exit;
try
FR:= FindFirst(Directory + '*.*', FaAnyfile, SR);
while FR = 0 do
begin
if ((SR.Attr and FaDirectory) = FaDirectory) and (SR.Name <> '.') and
(SR.Name <> '..') then
begin
if DeleteDirectory(Directory + SR.Name) = -1 then
Break;
end;
if ((SR.Attr and FaDirectory <> FaDirectory) and (SR.Attr and FaVolumeID<> FaVolumeID)) then
begin
FileSetAttr(Directory+ SR.Name,FileGetAttr(Directory+ SR.Name) and (not faReadOnly));
if not DeleteFile(PChar(Directory + SR.Name)) then
Break;
end;
FR:= FindNext(SR);
end;
FindClose(SR);
RemoveDirectory(PChar(Directory));
Result:= 0;
except
Result := -1;
end;
end;
function DeleteDirectory(Directory: string): Integer;
var
SR: TSearchRec;
FR: Integer;
begin
Result:= -1;
if not DirectoryExists(Directory) then
Exit;
try
FR:= FindFirst(Directory + '*.*', FaAnyfile, SR);
while FR = 0 do
begin
if ((SR.Attr and FaDirectory) = FaDirectory) and (SR.Name <> '.') and
(SR.Name <> '..') then
begin
if DeleteDirectory(Directory + SR.Name) = -1 then
Break;
end;
if ((SR.Attr and FaDirectory <> FaDirectory) and (SR.Attr and FaVolumeID<> FaVolumeID)) then
begin
FileSetAttr(Directory+ SR.Name,FileGetAttr(Directory+ SR.Name) and (not faReadOnly));
if not DeleteFile(PChar(Directory + SR.Name)) then
Break;
end;
FR:= FindNext(SR);
end;
FindClose(SR);
RemoveDirectory(PChar(Directory));
Result:= 0;
except
Result := -1;
end;
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
网易云信
2023-12-06 广告
2023-12-06 广告
网易云信提供一站式的 1 对 1 UIKit 组件库,可以更快地搭建 1 对 1 社交平台,能够快速实现音视频呼叫、音视频通话、1对1消息发送、美颜和礼物功能,直接可以复用我们的组件源码就可以了。优势:1、全套1对1 UI组件,接入更快;2...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询