无法使用deletefile命令删除文件夹

Oma*_*bal 2 delphi

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not deletefile('c:\test') then
  raiselastoserror
end;
Run Code Online (Sandbox Code Playgroud)

我得到操作系统错误5:访问被拒绝当我使用相同的代码删除文件说wwj.txt它工作正常,但不适用于文件夹我做错了什么?

mj2*_*008 5

请改用RemoveDir()过程.确保它不是您的应用程序的当前目录,或任何其他目录,或者它将保留.必须使用SysUtils来获得该功能.

如果需要,请先删除目录中的内容(如下).可以递归删除,并考虑'.'的含义.测试您是否使用带有'.'的目录或文件.

procedure DeleteFiles( szDBDirectory : string );
var
    szFile : string;
    SearchRec: TSearchRec;
    szSearchPath : string;
    nResult : integer;
begin
    szSearchPath := szDBDirectory;
    nResult := FindFirst(szSearchPath + '\*.*', faAnyFile, SearchRec);
    try
        while 0 = nResult do
        begin
            if('.' <> SearchRec.Name[1]) then
            begin
                szFile := szSearchPath + '\' + SearchRec.Name;
{$IFDEF DEBUG_DELETE}
                CodeSite.Send('Deleting "' + szFile + '"');
{$ENDIF}
                FileSetAttr(szFile, 0);
                DeleteFile(szFile);
            end;

            nResult := FindNext(SearchRec);
        end;
    finally
        FindClose(SearchRec);
    end;
end;
Run Code Online (Sandbox Code Playgroud)