uses ShellAPI var p:_shfileopstruct; a:integer; begin p.Wnd:=handle; p.wFunc:=FO_delete; p.pFrom:='f:\test'#0#0; //删除f盘的test文件夹 p.pTo:=nil; p.fFlags:=fof_noconfirmation; p.fAnyOperationsAborted:=true;end;
'f:\test'#0#0 后面的#0#0 是什么意思?
var Form1: TForm1; rec_stack:array [1..30] of TSearchRec; rec_pointer:integer; Del_Flag:Boolean; ------------------------------------------------------------------ ------procedure TForm1.DeleteTree(s:string); VAR searchRec:TSearchRec; begin if FindFirst(s+'\*.*', faAnyFile, SearchRec)=0 then repeat if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then begin if (SearchRec.Attr and faDirectory>0) then begin rec_stack[rec_pointer]:=SearchRec; rec_pointer:=rec_pointer-1; DeleteTree(s+'\'+SearchRec.Name); rec_pointer:=rec_pointer+1; SearchRec:=rec_stack[rec_pointer]; end else begin try FileSetAttr(s+'\'+SearchRec.Name,faArchive); DeleteFile(s+'\'+SearchRec.Name); except Application.MessageBox(PChar('Delete File:'+s+'\'+SearchRec.Name+' Error!'),'Info',MB_OK); Del_Flag:=False; end; end; end; until (FindNext(SearchRec)<>0); FindClose(SearchRec); if rec_pointer<30 then begin try FileSetAttr(s,faArchive); RemoveDir(s); except Application.MessageBox(PChar('Delete Directory:'+s+' Error!'),'Info',MB_OK); Del_Flag:=False; end; end; end; ---------------------------------------------------------Del_Flag:=True; rec_pointer:=30; DeleteTree('c:\temp'); if Del_Flag then Application.MessageBox(PChar('目录c:\temp的内容已成功清除!'),'信息',MB_OK);
a:=shfileoperation(p); 上述代码少了一句, 我已测试通过
放在p.fAnyOperationsAborted:=true;之后
var OpStruc: TSHFileOpStruct; DelDir:string; Begin DelDir:='C:\Temp' ; With OpStruc DO Begin Wnd:= Handle; wFunc:= FO_DELETE; pFrom:=pchar(DelDir); //fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; fFlags:= FOF_ALLOWUNDO ; fAnyOperationsAborted:= False; hNameMappings:= Nil; lpszProgressTitle:= Nil; end; ShFileOperation( OpStruc ); end;
var
p:_shfileopstruct;
a:integer;
begin
p.Wnd:=handle;
p.wFunc:=FO_delete;
p.pFrom:='f:\test'#0#0; //删除f盘的test文件夹
p.pTo:=nil;
p.fFlags:=fof_noconfirmation;
p.fAnyOperationsAborted:=true;end;
后面的#0#0
是什么意思?
rec_stack:array [1..30] of TSearchRec;
rec_pointer:integer;
Del_Flag:Boolean;
------------------------------------------------------------------ ------procedure TForm1.DeleteTree(s:string);
VAR searchRec:TSearchRec;
begin
if FindFirst(s+'\*.*', faAnyFile, SearchRec)=0 then
repeat
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
if (SearchRec.Attr and faDirectory>0) then
begin
rec_stack[rec_pointer]:=SearchRec;
rec_pointer:=rec_pointer-1;
DeleteTree(s+'\'+SearchRec.Name);
rec_pointer:=rec_pointer+1;
SearchRec:=rec_stack[rec_pointer];
end
else
begin
try
FileSetAttr(s+'\'+SearchRec.Name,faArchive);
DeleteFile(s+'\'+SearchRec.Name);
except
Application.MessageBox(PChar('Delete File:'+s+'\'+SearchRec.Name+' Error!'),'Info',MB_OK);
Del_Flag:=False;
end;
end;
end;
until (FindNext(SearchRec)<>0);
FindClose(SearchRec);
if rec_pointer<30 then
begin
try
FileSetAttr(s,faArchive);
RemoveDir(s);
except
Application.MessageBox(PChar('Delete Directory:'+s+' Error!'),'Info',MB_OK);
Del_Flag:=False;
end;
end;
end;
---------------------------------------------------------Del_Flag:=True;
rec_pointer:=30;
DeleteTree('c:\temp');
if Del_Flag then Application.MessageBox(PChar('目录c:\temp的内容已成功清除!'),'信息',MB_OK);
上述代码少了一句, 我已测试通过
OpStruc: TSHFileOpStruct;
DelDir:string;
Begin DelDir:='C:\Temp' ;
With OpStruc DO
Begin
Wnd:= Handle;
wFunc:= FO_DELETE;
pFrom:=pchar(DelDir);
//fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fFlags:= FOF_ALLOWUNDO ;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
ShFileOperation( OpStruc );
end;