¿CÓMO OBTENER UNA REFERENCIA A TODOS LOS PROCESOS HIJO DE UN PROCESO?
Modo de empleo:
Saludos.
Código (vbnet) [Seleccionar]
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the child processes of the source <see cref="Process"/>.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="p">
''' The source <see cref="Process"/>.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' A <see cref="IEnumerable(Of Process)"/> containing the child processes.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Sahred Iterator Function GetChildProcesses(ByVal p As Process) As IEnumerable(Of Process)
Dim scope As New ManagementScope("root\CIMV2")p.Id))
Dim options As New EnumerationOptions With {
.ReturnImmediately = True,
.Rewindable = False,
.DirectRead = True,
.EnumerateDeep = False
}
Using mos As New ManagementObjectSearcher(scope, query, options),
moc As ManagementObjectCollection = mos.Get()
For Each mo As ManagementObject In moc
Dim value As Object = mo.Properties("ProcessID").Value()
If (value IsNot Nothing) Then
Yield Process.GetProcessById(CInt(value))
End If
Next
End Using
End Function
Modo de empleo:
Código (vbnet) [Seleccionar]
Dim mainProcess As Process = Process.GetProcessesByName("explorer").Single()
Dim childProcesses As IEnumerable(Of Process) = GetChildProcesses(mainProcess)
For Each p As Process In childProcesses
Console.WriteLine(p.ProcessName)
Next
Saludos.