Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Process.GetProcessById (Int32, String)

 

Publicado: octubre de 2016

Devuelve un nuevo componente Process, dados un identificador de proceso y el nombre de un equipo en la red.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

public static Process GetProcessById(
	int processId,
	string machineName
)

Parámetros

processId
Type: System.Int32

Identificador de un recurso de proceso único en el sistema.

machineName
Type: System.String

Nombre de un equipo en la red.

Valor devuelto

Type: System.Diagnostics.Process

Componente Process asociado a un recurso de proceso remoto que se identifica mediante el parámetro processId.

Exception Condition
ArgumentException

El proceso especificado por el parámetro processId no se está ejecutando. Es posible que el identificador haya expirado.

O bien

La sintaxis de parámetro machineName no es válida. El nombre puede tener longitud cero (0).

ArgumentNullException

El parámetro machineName es null.

InvalidOperationException

Este objeto no inició el proceso.

Use this method to create a new T:System.Diagnostics.Process component and associate it with a process resource on a remote computer on the network. The process resource must already exist on the specified computer, because M:System.Diagnostics.Process.GetProcessById(System.Int32,System.String) does not create a system resource, but rather associates a resource with an application-generated T:System.Diagnostics.Process component. A process P:System.Diagnostics.Process.Id can be retrieved only for a process that is currently running on the computer. After the process terminates, M:System.Diagnostics.Process.GetProcessById(System.Int32,System.String) throws an exception if you pass it an expired identifier.

On any particular computer, the identifier of a process is unique. M:System.Diagnostics.Process.GetProcessById(System.Int32,System.String) returns one process at most. If you want to get all the processes running a particular application, use M:System.Diagnostics.Process.GetProcessesByName(System.String). If multiple processes exist on the computer running the specified application, M:System.Diagnostics.Process.GetProcessesByName(System.String) returns an array containing all the associated processes. You can query each of these processes in turn for its identifier. The process identifier can be viewed in the Processes panel of the Windows Task Manager. The PID column displays the process identifier that is assigned to a process.

If you do not specify a machineName, the local computer is used. Alternatively, you can specify the local computer by setting machineName to the value "." or to an empty string ("").

Win98WinMe

The machineName parameter is not supported on Windows 98 or Windows Millennium Edition (Windows Me).

The following example retrieves information of the current process, processes running on the local computer, all instances of Notepad running on the local computer, and a specific process on the local computer. It then retrieves information for the same processes on a remote computer.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();

            // Get all processes running on the local computer.
            Process[] localAll = Process.GetProcesses();

            // Get all instances of Notepad running on the local computer.
            // This will return an empty array if notepad isn't running.
            Process[] localByName = Process.GetProcessesByName("notepad");

            // Get a process on the local computer, using the process id.
            // This will throw an exception if there is no such process.
            Process localById = Process.GetProcessById(1234);


            // Get processes running on a remote computer. Note that this
            // and all the following calls will timeout and throw an exception
            // if "myComputer" and 169.0.0.0 do not exist on your local network.

            // Get all processes on a remote computer.
            Process[] remoteAll = Process.GetProcesses("myComputer");

            // Get all instances of Notepad running on the specific computer, using machine name.
            Process[] remoteByName = Process.GetProcessesByName("notepad", "myComputer");

            // Get all instances of Notepad running on the specific computer, using IP address.
            Process[] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");

            // Get a process on a remote computer, using the process id and machine name.
            Process remoteById = Process.GetProcessById(2345, "myComputer");
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
        }
    }
}

LinkDemand

for full trust for the immediate caller. This member cannot be used by partially trusted code.

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: