gpt4 book ai didi

delphi - 无法使用删除文件命令删除文件夹

转载 作者:行者123 更新时间:2023-12-03 18:04:04 25 4
gpt4 key购买 nike



procedure TForm1.Button1Click(Sender: TObject);
开始
如果不是 deletefile('c:\test') 那么
引发错误
结尾;

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

最佳答案

改用 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;

关于delphi - 无法使用删除文件命令删除文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1465542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com