Deleting a locked folder or file

I'm trying to delete a folder and when I delete it, a window pops up and the text in it is:

Операция не может быть завершена, так как эта папка или файл открыты в другой программе.
Закройте папку или файл и повторите попытку.

I tried using the KillTask function. But it kills the process itself (that is, it closes the file that holds the folder) and I just need to free the folder from the process that does not allow it to be deleted (But not kill the process itself that holds the folder). And then delete this folder.

Here this function is not suitable:

function KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

  while Integer(ContinueLoop) <> 0 do
  begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
      UpperCase(ExeFileName))) then
      Result := Integer(TerminateProcess(
                        OpenProcess(PROCESS_TERMINATE,
                                    BOOL(0),
                                    FProcessEntry32.th32ProcessID),
                                    0));
     ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

Can someone tell me a simple version in Delphi ?

Author: Gleb, 2020-07-11

1 answers

In 2 clicks, you will not do this, it is at the level of the system itself that this behavior is implemented. When a file is opened, it is held by the thread of the program that works with it.

Unloker also can not always delete a file, if it is occupied by a system process, it is suggested to delete it with a reboot.

Why is this functionality not available ? Everything is understandable: this leads to a violation of the logic of the programs.

In extreme cases, write a script to close the software, delete all temporary files. files and start the process back(bat). Either clear the cache before closing or running your software.

 1
Author: Azmandios, 2020-07-19 17:56:27