vb.net 杀死进程和父进程打开的每个子进程(如'TaskKill /F /T' 命令)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20313979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kill Process and each child processes opened by the parent process (Like 'TaskKill /F /T' command)
提问by ElektroStudios
I have an SFX WinRARautoxtraible file which runs a textfile using notepad.exe process, and the SFX file waits to receive an exitcode of notepad.exe to finish the SFX execution.
我有一个 SFX WinRARautoxtraible 文件,它使用 notepad.exe 进程运行文本文件,并且 SFX 文件等待接收 notepad.exe 的退出代码以完成 SFX 执行。
I would like to do a treekill, kill all the processes openen by the SFX file, or in other words, I would like to reproduce this Batch command, in .NET:
我想做一个treekill,杀死所有由 SFX 文件打开的进程,或者换句话说,我想重现这个批处理命令,在.NET:
/t : Specifies to terminate all child processes along with the parent process, commonly known as a tree kill.
/t :指定与父进程一起终止所有子进程,通常称为树杀。
Taskkill /F /T /IM "SFX file.exe"
To kill a process I do this, in VBNET:
为了杀死一个进程,我这样做,在VBNET:
''' <summary>
''' Kill a process specifying the process name.
''' </summary>
Private Sub Kill_Process(ByVal ProcessName As String,
Optional ByVal Recursive As Boolean = True)
ProcessName = If(ProcessName.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase),
ProcessName.Substring(0, ProcessName.LastIndexOf(".")),
ProcessName)
For Each p As Process In Process.GetProcessesByName(ProcessName)
p.Kill()
If Not Recursive Then Exit For
Next p
End Sub
Now, In C#or VBNEThow I could identify the child processes opened by the SFX to kill them all at once?
现在,在C#或VBNET如何识别 SFX 打开的子进程以一次性杀死它们?
采纳答案by Neolisk
Some ideas:
一些想法:
- Identify and Kill Child Process(WinAPI and WMI)
- How To End Process Tree(WinAPI)
- Kill process tree programatically in C#(StackOverflow - WMI)
- 识别并杀死子进程(WinAPI 和 WMI)
- 如何结束进程树(WinAPI)
- 在 C# 中以编程方式杀死进程树(StackOverflow - WMI)
The two approaches being used are ManagementObjectSearcher (WMI) and WinAPI.
使用的两种方法是 ManagementObjectSearcher (WMI) 和 WinAPI。
回答by ElektroStudios
I just want to share the VBNET modification that I've did to this C# solution Kill process tree programmatically in C#
我只想分享我对这个 C# 解决方案在 C# 中以编程方式杀死进程树所做的 VBNET 修改
''' <summary>
''' Kill a process, and/or all of it's children processes.
''' </summary>
''' <param name="ProcessName">
''' Indicates the name of the process to kill.
''' </param>
''' <param name="KillChildProcesses">
''' Indicates wether the child processes of the parent process should be killed.
''' </param>
''' <param name="RecursiveKill">
''' If set to False, only kills the first process found with the given process name,
''' otherwise, kills every process found with the same process name
''' (This option is usefull for multi-instance processes).
''' </param>
Private Sub Kill_Process(ByVal ProcessName As String,
Optional ByVal KillChildProcesses As Boolean = False,
Optional ByVal RecursiveKill As Boolean = True)
' Set correctly the given process name
ProcessName = If(ProcessName.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase),
ProcessName.Substring(0, ProcessName.LastIndexOf(".")),
ProcessName)
For Each p As Process In Process.GetProcessesByName(ProcessName)
Select Case KillChildProcesses
Case False ' Kill only this process.
p.Kill()
Case True ' Kill this process and it's children processes.
For Each mo As ManagementObject
In New ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" & Convert.ToString(p.Id)).[Get]()
Kill_Process(mo("Name"), True, RecursiveKill)
Next mo
p.Kill()
End Select
' Don't continue searching processes with the same name.
If Not RecursiveKill Then Exit Sub
Next p
End Sub

