In your case, you need check all process handler list to find which process locks this file and then close the handler via DLL injection. But it may cause other process unstable. So using Windows API MoveFileEx is an acceptable solution. This API can accept a parameter “MOVEFILE_DELAY_UNTIL_REBOOT ” to remove the file only after reboot. Behind this API, it just adds a value to record the pending deleted files under the registry entry “[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations]”. Windows will detect this registry entry after reboot and delete all recorded files.
P/Invoke allows us to use managed code to call an unmanaged function implemented in an unmanaged dll. For more information about P/Invoke, please see " Platform Invoke Tutorial ". The example code below can show you how to call MoveFileEx via C#: