Get partial name of a process

2

Is there any way I can get a process that is running by a partial name, like what happens with like in a SQL search?

Why I ask this: I have a team viewer custom here for the company. The problem is that if I happen to have a process named "Team Viewer 10", I can kill > it. But if the client has a newer or earlier version, the process name is not the same ... So I would not have kill .

Then how would I get the name of the process partially and if found, kill? That is, if I find a process with the name "Team Viewer" already trigger the code to kill this process?

What I have so far:

 //buscando o nome do processo
        System.Diagnostics.Process[] processoTv = System.Diagnostics.Process.GetProcessesByName(teamViewer);

        //verificando se existe o processo em execução e se houver finaliza
        if (processoTv.Length > 0)
            foreach (System.Diagnostics.Process processoItem in processoTv)
                processoItem.Kill();

Where teamViewer is equal to "Team Viewer 10".

Is there any way to do this?

    
asked by anonymous 07.01.2016 / 20:40

2 answers

2

Get All Processes and manually filter :

Process[] processoTv = Process.GetProcesses(); //variável só necessária se pretente usar mais tarde
foreach (var processoItem in processoTv) {
    if (processoItem.ProcessName.Contains(teamViewer)) {
        processoItem.Kill();
    }
}

I do not know all the implications of doing this. This is the base, but may have something that might give some trouble, I have not read all the documentation, which is something that every programmer should do before using anything. I did not do it because it's not something I'm going to use. But there's the caveat.

I've used it to simplify the code. You can do the filter with LINQ too, but it does not compensate because you will have to foreach .

    
07.01.2016 / 20:51
2

You can try this:

  foreach (var process in System.Diagnostics.Process.GetProcesses())
        {
            if (process.ProcessName.Contains("Team Viewer"))
            {
                process.Kill();

            }

        }
    
07.01.2016 / 20:53