相关疑难解决方法(0)

Delphi,删除带内容的文件夹

当我在文件夹中有子文件夹时 - 此代码不是删除文件夹...是否有任何错误?

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;
Run Code Online (Sandbox Code Playgroud)

windows delphi delphi-7

10
推荐指数
4
解决办法
2万
查看次数

删除具有非空子目录和文件的目录

如何删除一个包含某些文件和一些非空子目录的目录.
我试过SHFileOperation函数.它在Windows 7中存在一些兼容性问题.
然后我尝试了IFileOperation接口.但它在Windows XP中不兼容.然后我按照David Heffernan的建议尝试了以下代码:

procedure TMainForm.BitBtn01Click(Sender: TObject);
var
  FileAndDirectoryExist: TSearchRec;
  ResourceSavingPath : string;
begin
  ResourceSavingPath := (GetWinDir) + 'Web\Wallpaper\';
  if FindFirst(ResourceSavingPath + '\*', faAnyFile, FileAndDirectoryExist) = 0 then
  try
    repeat
      if (FileAndDirectoryExist.Name <> '.') and (FileAndDirectoryExist.Name <> '..') then
        if (FileAndDirectoryExist.Attr and faDirectory) <> 0 then
          //it's a directory, empty it
          ClearFolder(ResourceSavingPath +'\' + FileAndDirectoryExist.Name, mask, recursive)
        else
          //it's a file, delete it
          DeleteFile(ResourceSavingPath + '\' + FileAndDirectoryExist.Name); …
Run Code Online (Sandbox Code Playgroud)

delphi

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

delphi ×2

delphi-7 ×1

windows ×1