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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:18:28  来源:igfitidea点击:

Delphi, delete folder with content

windowsdelphidelphi-7

提问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).

TDirectoryis found in IOUtilswhich is quite a recent RTL addition.

TDirectory发现IOUtils其中是最近添加的 RTL。

The Trueflag is passed to the Recursiveparameter 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_ALLOWUNDOflag, 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

The last time I needed to delete a folder with content I used the JCL:

上次我需要删除包含内容的文件夹时,我使用了JCL

uses JclFileUtils;

DeleteDirectory(DirToDelete, True);

The last parameter tells whether the files should go to the recycle bin or not, which is a nice bonus.

最后一个参数告诉文件是否应该进入回收站,这是一个很好的奖励。

回答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; 

回答by gabr

uses DSiWin32;

DSiDeleteTree(folderName, false);

DSiWin32is open source project relased with "use as you wish" license.

DSiWin32是一个带有“随心所用”许可的开源项目。