2

No, this is not Malware. I'm trying to make a PC helper tool that kills all tasks that are non-Windows processes, but in the process, it kills itself.

It's a console application, so I tried disincluding cmd.exe to kill, but it still kills my program.

Is there a way kind of like this?

if (process.ToString == this.ExecutableName)
3
  • are you saying that you want a way to identify your own process? Commented Aug 1, 2014 at 18:52
  • I think you want to find out what the assembly name is Commented Aug 1, 2014 at 18:52
  • Sam, yes. That is what I'm asking. gunr, I will look at that now. Commented Aug 1, 2014 at 18:54

5 Answers 5

3

It doesn't get much easier than

Process self = Process.GetCurrentProcess() ;
foreach( Process p in Process.GetProcesses().Where( p => p.Id != self.Id ) )
{
  p.Kill() ;
}

If you need to worry about your parent process (so you don't kill the command shell that launched your process, the answers to the question "How can I get the PID of the parent process of my application" should guide you.

Sign up to request clarification or add additional context in comments.

Comments

0

If you run the application from the solution folder, your process will be easily identifiable in the task manager eg: consoleApplication1.exe

You can get the name of the process by:

var cb = Assembly.GetExecutingAssembly().CodeBase;
var processName = Path.GetFileName(cb);

You will see that in your list of processes in task manager, the name of the process is different if you are debugging and you have "Enable the visual studio hosting process" checked. In this instance the process name would be

consoleApplication1.vshost.exe

You can uncheck this option by right clicking on your project and clicking on "Properties" and then selecting "Debug". You should see a checkbox saying "Enable the visual studio hosting process". Deselect the check box.

Your process name will then be

consoleApplication1.exe

Comments

0
List<string> safeProcs = new List<string>() { "App1", "App2", "App3" };

List<Process> procs = Process.GetProcesses()
    .Where(p => !p.Equals(Process.GetCurrentProcess()) && !safeProcs.Contains(p.ProcessName))
    .ToList();
procs.ForEach(p => p.Kill());

2 Comments

I think this will kill windows processes as well (or at least attempt to).
yes but it sounded like that's what he was asking even though it won't actually kill everything like that
0

I think you should not use (.exe) in your process name and you must use (using System.Linq;) not to make any error while using (.Any()).

if (Process.GetProcessesByName("YourProcessNameGoesHere").Any())
            {
                
Process MyCurentProcess = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName("YourProcessNameGoesHere").Where(process => process.Id != MyCurentProcess.Id))
                {
                    process.Kill();
                }
            }

Comments

-1

You can use this procedure:

    private void KillProcess(string _process)
    {
        Process[] procs = Process.GetProcesses();

        foreach (Process proc in procs)
        {
            if (proc.ProcessName == _process)
            {
                proc.Kill();
                proc.WaitForExit();
            }
        }
    }

then:

KillProcess(this.ExecutableName);

1 Comment

This will kill only the given _process, not everything except the given process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.