I do the following:
Copy an executable to
C:\temp\x.exe
Start the executable with
System.Diagnostics.Process.Start
and then wait for the process to exit, synchronously, by callingWaitForExit
on theProcess
object returned byStart
.Delete the executable
C:\temp\x.exe
On some machines, this works great, but on others, the call to DeleteFile
fails, because the file is still in use. So it seems that once WaitForExit
returns, it does not imply that Windows is done with the EXE.
What are my options here? The obvious one whould be to try again the DeleteFile
a few milliseconds later, in a loop, until deletion succeeds or the loop times out. But is there a cleaner way to wait for a file to be closed by everyone?