Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Process Killing

I need to write a program in c# that would just start, kill one process\exe that it is supposed to kill and end itself.

The process I need to kill is another C# application so it is a local user process and I know the path to the exe.

like image 764
Moon Avatar asked Feb 10 '10 14:02

Moon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Check out Process.GetProcessesByName and Process.Kill

// Get all instances of Notepad running on the local
// computer.
Process [] localByName = Process.GetProcessesByName("notepad");
foreach(Process p in localByName)
{
   p.Kill();
}
like image 115
SwDevMan81 Avatar answered Sep 29 '22 20:09

SwDevMan81


First search all processes for the process you want to kill, than kill it.

Process[] runningProcesses = Process.GetProcesses();
foreach (Process process in runningProcesses)
{
    // now check the modules of the process
    foreach (ProcessModule module in process.Modules)
    {
        if (module.FileName.Equals("MyProcess.exe"))
        {
            process.Kill();
        }
    }
}
like image 33
Simon Linder Avatar answered Sep 29 '22 19:09

Simon Linder