none
How to terminate a process in c# RRS feed

  • Question

  • Hey everyone, I am currently trying to find a way to terminate the program "utorrent" using my c# program, does anyone have any idea how to do this?
    Friday, July 10, 2009 12:42 PM

Answers

  • Doesn't GetProcessesByName return an array?

    try
    {
    	Process [] proc Process.GetProcessesByName("utorrent");
    	proc[0].Kill();
    }

    Thursday, February 9, 2012 4:44 AM
  • Use this to close all the process called utorrent

    try
    {
    	foreach (Process proc in Process.GetProcessesByName("utorrent"))
                {
                    proc.Kill();
                }
    }
    catch(Exception ex)
    {
    	MessageBox.Show(ex.Message);
    }


    Thursday, July 19, 2012 2:17 AM

All replies

  • Hi,

     

    Process[] prs = Process.GetProcesses();
    
    
    foreach (Process pr in prs)
    {
    if (pr.ProcessName == "utorrent")
    {
    
    pr.Kill();
    
    }
    
    }
    

    Hope this will help u...........

     


    Nagarjuna Dilip
    • Proposed as answer by NagarjunaDilip Tuesday, July 14, 2009 12:05 PM
    Friday, July 10, 2009 12:48 PM
  • try this..

    foreach(System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
    {
    if (myProc.ProcessName == "process name")
    {
    myProc.Kill();
    }
    }

     

     

     


    :: Learning .Net ::
    Friday, July 10, 2009 12:48 PM
  • Hi - I think my information can help.

    To only kill() one running process, check out the C# code-snippet below:
    ================================================================
    try
    {
          Process proc = Process.GetProcessesByName("utorrent");
          proc.Kill();
    }
    catch (Exception ex)
    {
          MessageBox.Show(ex.Message.ToString());
    }

    ================================================================

    I hope this information was helpful...

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Friday, July 10, 2009 1:11 PM
  • Thanks, this really helped out.
    Friday, July 10, 2009 2:11 PM
  • An extra point for Prasant Swain...

    With the first solutions, I got the error:

    Error 1 The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?

    I am new to this programming environment. It would have taken me a while to decipher that error - But - Since you specified the full "System.Diagnostics.Process", I was able to add "using System.Diagnostics;" before the namespace to get it working.

    By default, Visual Studio 2008 has:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    Adding using System.Diagnostics; makes it work.

    Tuesday, June 21, 2011 2:52 PM
  • Doesn't GetProcessesByName return an array?

    try
    {
    	Process [] proc Process.GetProcessesByName("utorrent");
    	proc[0].Kill();
    }

    Thursday, February 9, 2012 4:44 AM
  • Use this to close all the process called utorrent

    try
    {
    	foreach (Process proc in Process.GetProcessesByName("utorrent"))
                {
                    proc.Kill();
                }
    }
    catch(Exception ex)
    {
    	MessageBox.Show(ex.Message);
    }


    Thursday, July 19, 2012 2:17 AM
  • Shouldn't the try-catch be inside the loop?
    Tuesday, November 12, 2013 10:11 PM
  • No, because also GetProcessesByName may fault 
    Thursday, December 19, 2013 6:43 PM
  • try
    {
    	Process [] proc = Process.GetProcessesByName("utorrent");  //forgot "="
    	proc[0].Kill();
    }
    Wednesday, September 9, 2015 1:21 PM
  • There is no Kill method in there. I don't get why not. Everyone says there is but it's not showing up, at least not in the Intellisense menu.
    Tuesday, July 25, 2017 5:48 PM
  • There is no Kill method in there. I don't get why not. Everyone says there is but it's not showing up, at least not in the Intellisense menu.

    See in MSDN  (Process.Kill Method ())

    Tuesday, July 25, 2017 5:58 PM
  • try

          System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();


                foreach (System.Diagnostics.Process pr in prs)
                {
                    if (pr.ProcessName == "utorrent")
                    {

                        pr.Kill();

                    }

    Saturday, March 2, 2019 9:24 AM
  • It worked like a charm. I use WordPad to display reports and to re-generate the report I needed to close the WordPad.  I tries many other methods to see if the file is in use or even to delete the file before regenerating it, but none of them worked. Thanks a million for this simple and elegant solution.  

    Friday, July 10, 2020 7:47 PM