Taking a partial name from a process

Asked

Viewed 268 times

2

Is there any way that I can get a process that’s running under a partial name, like what happens to like in a survey SQL?

'Cause I ask this, I got one team viewer customized here for the company. Only that the version we have here is the 10. Beauty so far without problems... What happens is that if you happen to have a process named "Team Viewer 10", I can kill he. Only if the client has a newer or earlier version, the name of the process is not the same... Then there would be no way kill.

So there’s a way I could take the name of the lawsuit in a partial way and if found, kill? That is, if I find a process with the name "Team Viewer", already trigger the code to kill that 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 that?

2 answers

2


Take all the files and manually insert:

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 put in the Github for future reference.

I don’t know the full implications of doing this. This is the basis, but there may be something that can give some trouble, I haven’t read all the documentation, which is something every programmer should do before using anything. I didn’t do it because it’s not something I’m going to use, but there’s the caveat.

I took the opportunity to simplify the code. You can make the filter with LINQ too, but it doesn’t pay because then you will have to perform the foreach.

  • Process[] processoTv = Process.GetProcesses(); //variável só necessária se pretente usar mais tarde.... Later how? Like if I want to check the processes running at some point?

  • Variable should essentially be used to create an intermediate state. Although it may be useful to create a variable to use only once, in general, this is not necessary. Of course, if something is too complicated, it’s good to create a variable just to document, but it’s rare. If you’re going to use that only once in the algorithm, don’t create a variable just for that, use the direct expression. Most programmers do not understand the use of the variable, which is one of the most basic features of code. The problem may be the examples that to be didactic put redundancy. It is not to copy.

  • I managed to solve using your solution! Thank you very much!

2

You can try that:

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

            }

        }
  • Good solution! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.