The problem is that you are not the only one who processes your file. Other processes and services may also open the file. Therefore, removal is not possible until they release their handles. You can rename the file while these descriptors are open. You can copy the file while these handles are open. Not sure if you can move the file to another container?
Other esp processes and services. including antivirus, indexing, etc.
, " " Windows:
bool DeleteFileNow(const wchar_t * filename)
{
if (!PathFileExistsW(filename))
return false;
wchar_t path[MAX_PATH];
wcscpy_s(path, filename);
PathRemoveFileSpecW(path);
wchar_t tempname[MAX_PATH];
if (!GetTempFileNameW(path, L".xX", 0, tempname))
return false;
if (!MoveFileExW(filename, tempname, MOVEFILE_REPLACE_EXISTING))
{
DeleteFileW(tempname);
return false;
}
return DeleteFileW(tempname) != FALSE;
}