I have a big problem here and unable to fix it. I'm working on an update software at work who need to work even on a computer with running processes. So if a copy or other treatment cause exceptions, I need to kill the process that cause the error and retry and continue the update. The problem is I kill theses processes but windows continue to say to me that there is some processes opened even if I killed them.
So I have a method that copy files of the update on a computer where processes are running. I used Process.GetProcessesByName("processName") and solution below to kill processes.
Using C#, how does one figure out what process locked a file?
This is my code:
try
{
file.CopyTo(destAbsolutePath, true);
}
catch (IOException ioException)
{
foreach (var process in Process.GetProcessesByName(file.Name.Replace(Path.GetExtension(file.Name), "")))
{
process.Kill();
}
foreach (Process p in Win32Processes.GetProcessesLockingFile(file.FullName))
{
p.Kill();
}
file.CopyTo(destAbsolutePath, true);
}
I ask to me if Windows need time to know if a process is killed. I tried to wait a little bit but nothing happen. If I close my application after have killed these processes and reopen it, there is no more error. If I kill my application with GetCurrentProcess.Kill()
, the error stay there. I don't think it's my application that cause the IOException because i'm only looping over FileInfo object.
I tried this solution to check if my file (a .dll custom file used by processes I killed) was locked after killing the processes who use my .dll file and it wan't locked:
Is there a way to check if a file is in use?
Please, help me. Thanks