如何在C#中根据特定的ID终止进程

huangapple go评论65阅读模式
英文:

How to kill process by specific Id in c#

问题

我有多个同时运行的进程与我的C#程序,所有这些进程都具有相同的名称,但具有不同的ID,我想通过它们的特定ID来终止我的程序的特定进程,或者重命名它们并按其特定名称终止它们。

英文:

I have multiple processes that running at the same time of my C# program and all of this processes have the same name with different ID and I want to kill specific process of my program by their specific ID or rename them and kill it by their specific name.

答案1

得分: 3


Edit:
请注意文档的 NotesRemarks 部分,特别是对于 Kill 方法。

英文:

Edit:
Pay attention to Notes and Remarks sections of the documentation, specially for the Kill method.

答案2

得分: 0

如果您拥有ID,可以像这样执行操作(其中id是一个int32):

Process process = Process.GetProcessById(id);
process.Kill();

或者,如果您正在使用LinQ,可以像这样执行操作:

Process.GetProcesses()
            .Where(x => x.Id.Equals(id))
            .FirstOrDefault()
            .Kill();
英文:

If you have the id you can do it like this (where id is an int32):

Process process = Process.GetProcessById(id);
process.Kill();

Or if you are using LinQ you can do it like this:

Process.GetProcesses()
            .Where(x => x.Id.Equals(id))
            .FirstOrDefault()
            .Kill();

huangapple
  • 本文由 发表于 2023年2月6日 19:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定