1 year ago

#296685

test-img

KiraKo

C# : Killing a specific file opened and not all instance

I'm trying to open an excel file in my application via Process. When the form is closed I want to close that file automatically . The issue that I'm facing is even if I specify the PID process of the Excel opened . When I run the kill instruction, all the other excels file opened will close and not specifically the one that I need.

Could you please help me .

 File.WriteAllBytes(filePath, bytes);
 proc = new Process();
 proc = Process.Start(filePath);
 processId = proc.Id; //keep PID to kill that process 

  proc.WaitForInputIdle(); //Set focus on opened file
  SetForegroundWindow(this.Handle);                        
  proc.Dispose();
// killing the process after file use
  Process processes = Process.GetProcessById(processId);
  processes.Kill();

c#

file

process

kill

taskkill

0 Answers

Your Answer

Accepted video resources