Kill Process and each child processes opened by the parent process (Like 'TaskKill /F /T' command)

StackOverflow https://stackoverflow.com/questions/20313979

Frage

I have an SFX WinRAR autoxtraible 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.

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:

/t : Specifies to terminate all child processes along with the parent process, commonly known as a tree kill.

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

To kill a process I do this, in 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 VBNET how I could identify the child processes opened by the SFX to kill them all at once?

War es hilfreich?

Lösung

Some ideas:

The two approaches being used are ManagementObjectSearcher (WMI) and WinAPI.

Andere Tipps

I just want to share the VBNET modification that I've did to this C# solution 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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top