1.既然是windows平台上, 就用shell API吧. 下面是一个例子:
PHP 代码:
uses ShellAPI;
function DeleteDirectory(const sDir : string) : Boolean;
var
SHFileOpStruct : TSHFileOpStruct;
FromBuf : Array [0..255] of Char;
ToBuf : Array [0..255] of Char;
begin
//make sure the target directory existing
if not DirectoryExists(sDir) then
begin
Result := False;
function DeleteDirectory(const sDir : string) : Boolean;
var
SHFileOpStruct : TSHFileOpStruct;
FromBuf : Array [0..255] of Char;
ToBuf : Array [0..255] of Char;
begin
//make sure the target directory existing
if not DirectoryExists(sDir) then
begin
Result := False;
exit;
end;
//Initialize SHFileOpStruct.
Fillchar(SHFileOpStruct, Sizeof(SHFileOpStruct), 0);
FillChar(FromBuf, Sizeof(FromBuf), 0);
FillChar(ToBuf, Sizeof(ToBuf), 0);
StrPCopy(FromBuf, sDir);
StrPCopy(ToBuf, '');
//Delete directory and all its subdirectory as well as files.
//fill out the shell file operation struct.
With SHFileOpStruct Do
Begin
Wnd := 0;
end;
//Initialize SHFileOpStruct.
Fillchar(SHFileOpStruct, Sizeof(SHFileOpStruct), 0);
FillChar(FromBuf, Sizeof(FromBuf), 0);
FillChar(ToBuf, Sizeof(ToBuf), 0);
StrPCopy(FromBuf, sDir);
StrPCopy(ToBuf, '');
//Delete directory and all its subdirectory as well as files.
//fill out the shell file operation struct.
With SHFileOpStruct Do
Begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := @FromBuf;
pTo := @ToBuf;
fFlags := FOF_ALLOWUNDO;
fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
End;
//execute the operation and return result.
Result := (SHFileOperation(SHFileOpStruct) = 0);
pFrom := @FromBuf;
pTo := @ToBuf;
fFlags := FOF_ALLOWUNDO;
fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
End;
//execute the operation and return result.
Result := (SHFileOperation(SHFileOpStruct) = 0);
end;
两点注意:
1. 要应用shellapi
2. SHFileOperation(...)可以执行copy, move, rename和delete命令. file或folder/directory对它而言都是一个file object, 没有区别. 具体参数控制可参见MSDN.
1. 要应用shellapi
2. SHFileOperation(...)可以执行copy, move, rename和delete命令. file或folder/directory对它而言都是一个file object, 没有区别. 具体参数控制可参见MSDN.
方法二
//删除指定的目录或文件
//Undo默认为false,即直接删除,不能undo,可以选择true,删除到回收站
function nDeleteDir(SrcDir: String;UndoMK:boolean=false):Boolean;
var
FS: TShFileOpStruct;
begin
FS.Wnd := Application.Handle; //应用程序句柄
FS.wFunc := FO_DELETE; //表示删除
FS.pFrom := PChar(SrcDir+#0#0);
FS.pTo := nil;
if UndoMK
then FS.fFlags := FOF_NOCONFIRMATION + FOF_SILENT + FOF_ALLOWUNDO
// 表示删除到回收站
else FS.fFlags := FOF_NOCONFIRMATION + FOF_SILENT;
// 表示不删除到回收站
FS.lpszProgressTitle := nil;
Result := (ShFileOperation(FS) = 0);
End;
方法三
在uses中加入ShellApi
自定义下过程
procedure TForm1.Deletedir(str:string);
Var
T:TSHFileOpStruct;
P:String;
begin
P:='c:\update';//这里改成你要删除的任意目录名,P:=str str 是传过来的目录路径
With T do
Begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(P);
pTo:=nil;
delphi app
自定义下过程
procedure TForm1.Deletedir(str:string);
Var
T:TSHFileOpStruct;
P:String;
begin
P:='c:\update';//这里改成你要删除的任意目录名,P:=str str 是传过来的目录路径
With T do
Begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(P);
pTo:=nil;
delphi app
fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息
hNameMappings:=nil;
lpszProgressTitle:='正在删除文件夹';
fAnyOperationsAborted:=False;
End;
SHFileOperation(T);
Application.MessageBox('删除成功!','系统提示',64);
end;
hNameMappings:=nil;
lpszProgressTitle:='正在删除文件夹';
fAnyOperationsAborted:=False;
End;
SHFileOperation(T);
Application.MessageBox('删除成功!','系统提示',64);
end;
方法四:shellexecute(0, nil, 'rd', 'c:\update /Q /S', '', 0);
方法五:
创建文件夹可以用ForceDirectories函数,可以创建深层文件夹;删除文件夹,可以用到下面的函数,参数为文件夹的路径,如:d:\show
uses ShellAPI;
function DeleteDirectory(p :String): boolean;
var
f: TSHFILEOPSTRUCT;
begin
FillChar(f, SizeOf(f), 0);
with f do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(p+#0);
pTo := PChar(p+#0);
fFlags := FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;
end;
Result := (SHFileOperation(f) = 0);
end;
要简单一点的可以用Winexec调用dos的rd命令,如;winexec('cmd /c rd /s /q d:\show',sw_hide)就一句代码
遇到con文件夹可以先用winexec('cmd /c rd /s /q d:\show\con\',sw_hide)把con文件夹删除,再用winexec('cmd /c rd /s /q d:\show',sw_hide)把show删除
until FindNext(sr) <> 0;
FindClose(sr);
end;
RemoveDir(sPath);
end;
判断文件夹存在不
if not DirectoryExists("D:\APP1\SetUp") then
showmessage("存在") else
showmessage("不存在");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论