如何删除一个包含文件的目录?(50分)

  • 主题发起人 主题发起人 DelphiNewer168
  • 开始时间 开始时间
D

DelphiNewer168

Unregistered / Unconfirmed
GUEST, unregistred user!
如何删除一个包含文件的目录?
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, <br>&nbsp; Controls, Forms, Dialogs, &nbsp;StdCtrls, Buttons,<br>&nbsp; ShellAPI;//要引用ShellApI<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; BitBtn1: TBitBtn;<br>&nbsp; &nbsp; procedure BitBtn1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>var<br>&nbsp; T:TSHfileOpStruct;//要引用ShellApI<br><br>&nbsp; P:String;<br>begin<br>&nbsp; P:='C:/My Documents/test/abc';//要写完整路径。<br>&nbsp; with T do<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;Wnd := 0;<br>&nbsp; &nbsp; &nbsp;wFunc := FO_DELETE;<br>&nbsp; &nbsp; &nbsp;pFrom := pchar(p);<br>&nbsp; &nbsp; &nbsp;pTo := nil;<br>&nbsp; &nbsp; &nbsp;fFlags := FOF_ALLOWUNDO;<br>&nbsp; end;<br>&nbsp; SHFileOperation(T);<br>end;<br><br>end.<br><br>
 
这个API在删除的时候会提示往回收站删除的对话框,怎么样可以不提示?
 
procedure TForm1.BitBtn1Click(Sender: TObject);<br>var<br>&nbsp; T:TSHfileOpStruct;//要引用ShellApI<br><br>&nbsp; P:String;<br>begin<br>&nbsp; P:='C:/My Documents/test/abc';//要写完整路径。<br>&nbsp; with T do<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;Wnd := 0;<br>&nbsp; &nbsp; &nbsp;wFunc := FO_DELETE;<br>&nbsp; &nbsp; &nbsp;pFrom := pchar(p);<br>&nbsp; &nbsp; &nbsp;pTo := nil;<br>&nbsp; &nbsp; &nbsp;fFlags := FOF_ALLOWUNDO or FOF_SILENT or FOF_NOCONFIRMATION;<br>&nbsp; end;<br>&nbsp; SHFileOperation(T);<br>end;<br><br>end.<br><br>
 
看delphi自带的例程
 
用递归的方法写一个函数即可<br>&nbsp; 你用google搜一下delphi+递归会有所发现
 
最简单的方法:<br>NT下用 cmd /c rd /s "你的目录"<br>98下用 deltree /y "你的目录"
 
试试这个吧<br>procedure DelTree(Path:String);<br>Var<br>&nbsp;Found:Integer;<br>&nbsp;SearchRec:TSearchRec;<br>begin<br>&nbsp; Found := FindFirst(path+'*.*', $0000003F, SearchRec);<br>&nbsp; While Found = 0 Do<br>&nbsp; Begin<br>&nbsp; &nbsp; If ((SearchRec.Name&lt;&gt;'.') &nbsp;And (SearchRec.Name&lt;&gt;'..') )Then<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; If (SearchRec.Attr and $00000010)&lt;&gt; 0 Then<br>&nbsp; &nbsp; &nbsp; &nbsp; DelTree(Path+SearchRec.Name+'/')<br>&nbsp; &nbsp; &nbsp; Else<br>&nbsp; &nbsp; &nbsp; &nbsp; DeleteFile(Path+SearchRec.Name);<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; Found := FindNext(SearchRec);<br>&nbsp; End;<br>&nbsp; FindClose(SearchRec);<br>&nbsp; try<br>&nbsp; &nbsp; RmDir(Path);<br>&nbsp; except<br><br>&nbsp; end;<br>End;
 
简单递归一下就行了,这种问题也拿来问?
 
后退
顶部