Efficiency and performance should not worry you, IMO. You're probably not going to kill 1000 processes a second, so if one takes 10ms and the other takes 100ms, I doubt anyone will care.
So, the arguments for choosing one of the other is mostly ease of programming and maintaining. The API for enumerating processes is not the easiest, but you can find many example on the web (like here). Using tasklist will require obtaining the output and parsing it.
I'd go for the API for these reasons:
- You can get more information in case the operation fails. You'll know exactly where the problem was, and what was it.
- I'm not sure
tasklist
and taskkill
are guaranteed to be on every Windows machine. They do come with the OS, but someone might figure they enlarge the attack surface or whatever, and remove them.
- Parsing text is error prone. I have no idea, for instance, if
tasklist
's output depends on the OS locale. Do you?