Python killing script in C #

I am wondering if it is possible to kill a python script using C #.

In a small application I'm currently developing, the python application runs at localhost: portnumber. The application port number is always the same.

Is it possible, when the application is already running (I check this by getting a list of ports currently in use), to kill it with some command?

I already figured out that if it is not running I can start the application using Process.Start();

+3


source to share


1 answer


You can use Process.Kill () to kill a specific process ... To find out which process to kill you can use and run through them ...
GetProcesses()

For example, here's how you can kill the calculator (calc.exe):

 foreach (Process process in Process.GetProcesses().Where(p => 
                                                         p.ProcessName == "calc"))
 {
     process.Kill();
 }

      



This will find all processes named "calc" and kill them.

In your case, if you already have an object Process

(because you called Process.Start()

), you can specifically kill it with a method Kill()

.

+1


source







All Articles