【问题标题】:Kill Process and each child processes opened by the parent process (Like 'TaskKill /F /T' command)杀死进程和父进程打开的每个子进程(如'TaskKill /F /T'命令)
【发布时间】:2013-12-01 15:48:55
【问题描述】:

我有一个 SFX WinRAR autoxtraible 文件,它使用 notepad.exe 进程运行一个文本文件,并且 SFX 文件等待接收 notepad.exe 的退出代码以完成 SFX 执行。

我想做一个treekill,杀死所有由SFX文件打开的进程,或者换句话说,我想在.NET中重现这个批处理命令:

/t :指定终止所有子进程以及 父进程,俗称树杀。

Taskkill /F /T /IM "SFX file.exe"

为了杀死一个进程,我这样做了,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

现在,在 C#VBNET 中,我如何识别由 SFX 打开的子进程以一次性杀死它们?

【问题讨论】:

  • 喜欢this?
  • @Neolisk 非常感谢您提供的信息,子需要一个 pid,我正在尝试对其进行修饰以期望一个进程名称,我认为我可以在没有帮助的情况下自己完成,也许是这样代码救了我的命,再次感谢
  • 您应该能够调整 WMI 查询以按进程名称进行过滤。
  • 效果很好,谢谢
  • 我很高兴你能解决这个问题。 :)

标签: c# .net vb.net process child-process


【解决方案1】:

一些想法:

使用的两种方法是 ManagementObjectSearcher (WMI) 和 WinAPI。

【讨论】:

    【解决方案2】:

    我只想分享我对这个 C# 解决方案所做的 VBNET 修改Kill process tree programmatically in C#

    ''' <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
    

    【讨论】:

    • 顺便说一句,为您的代码编写 XML cmets 点赞。不过,我更喜欢对参数有更简短的解释。适合一行可打印的内容(通常为 100-120 个字符)。将其余部分包含在备注中。
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2015-02-18
    • 2016-03-05
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    相关资源
    最近更新 更多