Terminating a process using WMI

Hi,

I have been told that this is possible but I can't seem to find any sample code anywhere. Anyone know how to do this

Thanks.



Answer this question

Terminating a process using WMI

  • Robin Debreuil

    re-read the post. look at the last example as well as svens

  • D. Choquette

    you are aware that you can use the Process class to terminate a process right It would be cheaper also than using WMI, if its possible in WMI

  • toffyrn

    I need to be able to end remote processes as well, the process class is no good for that.


  • Tamizhan

    The problem that I am having is: How do I kill the process . I heard that it is possible with WMI but not with the Process class. Is this the case

    Thanks


  • N_John

    Thanks, your WMI sample is perfect! However, just out of interest, how can I do this using the Process class

    Thanks.


  • emepvsd

    pretty much how Sven posted, either doing it this way:

    Process[] theList = Process.GetProcesses("machineName")

    foreach(Process curProcess in theList)

    {

       if (curProcess.ProcessName.Equals("ProcessName"))

       {

          curProcess.Kill();

       }

    }

     

    or this way:

     

    Process[] theList = Process.GetProcessesByName("machineName", "ProcessName")

    foreach(Process curProcess in theList)

    {

       curProcess.Kill();

    }



  • angelblade27

    Oh damn ok sorry I haven't seen that you were getting the processes by machinename ok then i guess yours as well.
  • MichaelD!

    you can do it with both without problems. The only problem being is permission to kill a process on the remote computer. you need to have permission to do this and access rights

  • Melonheads Sister

    except your posting how to do it locally and sven is proposing a remote/local function.
  • Davids Learning

    using WMI, be sure you added a reference to System.Management.dll and imported the namespaces, System.Management and System.Management.Instrumentation.

    we then execute a query and execute the terminate command:

    ManagementScope theScope = new ManagementScope("\\\\ComputerName\\root\\cimv2");

    ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='ApplicationName.exe'");

    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);

    ManagementObjectCollection theCollection = theSearcher.Get();

    foreach (ManagementObject theCurObject in theCollection)

    {

    theCurObject.InvokeMethod("Terminate", null);

    }



  • maliger

    no worries ;-)

  • Keehan

    It is:

    Process.GetProcesses("remoteMachine") will give you a process collection of remoteMachine.

    Process.GetProcessByName("notepad", "remoteMachine") will give you a collection of processes named notepad on remoteMachine.

    Given that you have access rights on remoteMachine...

    --
    SvenC


  • Terminating a process using WMI