windows Delphi,删除包含内容的文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5716666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delphi, delete folder with content
提问by Emi
when I have subfolder in folder - this code isn't delete folders... Is there any error?
当我在文件夹中有子文件夹时-此代码不是删除文件夹...有什么错误吗?
procedure TForm.Remove(Dir: String);
var
Result: TSearchRec; Found: Boolean;
begin
Found := False;
if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
while not Found do begin
if (Result.Attr and faDirectory = faDirectory) AND (Result.Name <> '.') AND (Result.Name <> '..') then Remove(Dir + '\' + Result.Name)
else if (Result.Attr and faAnyFile <> faDirectory) then DeleteFile(Dir + '\' + Result.Name);
Found := FindNext(Result) <> 0;
end;
FindClose(Result); RemoveDir(Dir);
end;
回答by David Heffernan
The simplest thing to do is to call TDirectory.Delete(Dir, True)
.
最简单的方法是调用TDirectory.Delete(Dir, True)
.
TDirectory
is found in IOUtils
which is quite a recent RTL addition.
TDirectory
发现IOUtils
其中是最近添加的 RTL。
The True
flag is passed to the Recursive
parameter which means that the contents of the directories are empied before the directory is removed, an essential part of deleting directories.
该True
标志被传递给Recursive
参数,这意味着目录的内容在删除目录之前被清除,这是删除目录的重要部分。
In a comment you tell us that you use Delphi 7 and so this cannot be used.
在评论中,您告诉我们您使用的是 Delphi 7,因此无法使用。
Your code looks mostly fine. However, you don't mean:
您的代码看起来基本没问题。但是,您的意思不是:
(Result.Attr and faAnyFile <> faDirectory)
I think you mean:
我想你的意思是:
(Result.Attr and faDirectory <> faDirectory)
I would probably write it as follows:
我大概会这样写:
procedure TMyForm.Remove(const Dir: string);
var
Result: TSearchRec;
begin
if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
begin
Try
repeat
if (Result.Attr and faDirectory) = faDirectory then
begin
if (Result.Name <> '.') and (Result.Name <> '..') then
Remove(Dir + '\' + Result.Name)
end
else if not DeleteFile(Dir + '\' + Result.Name) then
RaiseLastOSError;
until FindNext(Result) <> 0;
Finally
FindClose(Result);
End;
end;
if not RemoveDir(Dir) then
RaiseLastOSError;
end;
回答by Andreas Rejbrand
If I were you, I'd just tell the operating system to delete the folder with all of its content. Do so by writing (uses ShellAPI
)
如果我是你,我只会告诉操作系统删除文件夹及其所有内容。写 ( uses ShellAPI
)
var
ShOp: TSHFileOpStruct;
begin
ShOp.Wnd := Self.Handle;
ShOp.wFunc := FO_DELETE;
ShOp.pFrom := PChar('C:\Users\Andreas Rejbrand\Desktop\Test\'#0);
ShOp.pTo := nil;
ShOp.fFlags := FOF_NO_UI;
SHFileOperation(ShOp);
[If you do
[如果你这样做
ShOp.fFlags := 0;
instead, you get a nice confirmation dialog. If you do
相反,您会看到一个不错的确认对话框。如果你这样做
ShOp.fFlags := FOF_NOCONFIRMATION;
you don't get the confirmation dialogue, but you do get a progress bar if the operation is lengthy. Finally, if you add the FOF_ALLOWUNDO
flag, you move the directory to the Waste Bin instead of permanently deleting it.
您不会看到确认对话框,但如果操作时间过长,您会看到进度条。最后,如果添加FOF_ALLOWUNDO
标志,则将目录移动到垃圾箱而不是永久删除它。
ShOp.fFlags := FOF_ALLOWUNDO;
Of course, you can combine flags as you like:
当然,您可以根据需要组合标志:
ShOp.fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO;
will not show any confirmation (but a progress dialog because you don't specify FOF_NO_UI
) and the directory will be moved to the waste bin and not permanently deleted.]
不会显示任何确认(但会显示进度对话框,因为您没有指定FOF_NO_UI
)并且该目录将被移动到垃圾箱并且不会被永久删除。]
回答by Heinrich Ulbricht
回答by Remy Lebeau
To address the original problem - try this:
要解决原始问题 - 试试这个:
procedure TForm.Remove(const Dir: String);
var
sDir: String;
Rec: TSearchRec;
begin
sDir := IncludeTrailingPathDelimiter(Dir);
if FindFirst(sDir + '*.*', faAnyFile, Rec) = 0 then
try
repeat
if (Rec.Attr and faDirectory) = faDirectory then
begin
if (Rec.Name <> '.') and (Rec.Name <> '..') then
Remove(sDir + Rec.Name);
end else
begin
DeleteFile(sDir + Rec.Name);
end;
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
RemoveDir(sDir);
end;