폴 더 삭제 / 삭제

코드:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, cxShellBrowserDialog, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    cxshlbrwsrdlg1: TcxShellBrowserDialog;//  
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure ClearDir(AHandle: THandle; const ADirName: String);   //uses ShellAPI
var
  SHFileOpStruct: TSHFileOpStruct;
  DirName: PChar;
  BufferSize: Cardinal;
begin
  BufferSize := Length(ADirName) + 1 + 1;
  GetMem(DirName, BufferSize);
  try
    FillChar(DirName^, BufferSize, 0);
    StrCopy(DirName, PChar(ADirName));
    with SHFileOpStruct do
    begin
      Wnd := AHandle;
      wFunc := FO_DELETE;
      pFrom := DirName;
      pTo := nil;
      fFlags := FOF_SILENT or FOF_ALLOWUNDO or FOF_NOCONFIRMATION;
      fAnyOperationsAborted := False;
      hNameMappings := nil;
      lpszProgressTitle := nil;
    end;
    SHFileOperation(SHFileOpStruct);
    MkDir(ADirName);//            
  finally
    FreeMem(DirName, BufferSize);
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  forderpath: string;
begin
  cxshlbrwsrdlg1.path := 'c:\';
  if not cxshlbrwsrdlg1.Execute then
  begin
    exit;
  end;
  forderpath := cxshlbrwsrdlg1.path;
  ClearDir(Self.Handle, forderpath);
end;

end.

 
function DeleteFolder(SourceDir: string): boolean;
var
  lpFileOp: TSHFileOpStruct;
begin
  if DirectoryExists(SourceDir) then
  begin
    zeromemory(@lpFileOp, SizeOf(TSHFileOpStruct));
    with lpFileOp do
    begin
      Wnd := Application.Handle;
      wfunc := FO_DELETE;
      pFrom := pchar(SourceDir + #0#0);
      pTo := nil;
      // fFlags := FOF_ALLOWUNDO or FOF_CONFIRMMOUSE;
      fFlags := FOF_SILENT or FOF_ALLOWUNDO or FOF_NOCONFIRMATION;
      hNameMappings := nil; // 'test';//SourceDir;
      lpszProgressTitle := nil;
      fAnyOperationsAborted := false;
    end;
    Result := SHFileOperation(lpFileOp) = 0;
  end;
end;

좋은 웹페이지 즐겨찾기