Process.GetProcessById Metodo

Definizione

Crea un nuovo componente Process e lo associa alla risorsa di processo esistente specificata.

Overload

GetProcessById(Int32)

Restituisce un nuovo componente Process, dato l'identificatore di un processo nel computer locale.

GetProcessById(Int32, String)

Restituisce un nuovo componente Process, dato un identificatore di processo e il nome di un computer sulla rete.

GetProcessById(Int32)

Restituisce un nuovo componente Process, dato l'identificatore di un processo nel computer locale.

public:
 static System::Diagnostics::Process ^ GetProcessById(int processId);
public static System.Diagnostics.Process GetProcessById (int processId);
static member GetProcessById : int -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer) As Process

Parametri

processId
Int32

Identificatore univoco nel sistema di una risorsa di processo.

Restituisce

Process

Componente Process associato alla risorsa locale del processo identificata dal parametro processId.

Eccezioni

Il processo specificato dal parametro processId non è in esecuzione. L'identificatore potrebbe essere scaduto.

Il processo non è stato avviato da questo oggetto.

Esempio

Nell'esempio seguente vengono recuperate le informazioni del processo corrente, i processi in esecuzione nel computer locale, tutte le istanze del Blocco note in esecuzione nel computer locale e un processo specifico nel computer locale. Recupera quindi le informazioni per gli stessi processi in un computer remoto.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
   // Get the current process.    
   Process^ currentProcess = Process::GetCurrentProcess();

   // Get all processes running on the local computer.
   array<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.
   array<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.
   array<Process^>^remoteAll = Process::GetProcesses("myComputer");

   // Get all instances of Notepad running on the specific computer, using machine name.
   array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
   
   // Get all instances of Notepad running on the specific computer, using IP address.
   array<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" );
}
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();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Sub BindToRunningProcesses()
            ' Get the current process. You can use currentProcess from this point
            ' to access various properties and call methods to control the process.
            Dim currentProcess As Process = Process.GetCurrentProcess()

            ' Get all processes running on the local computer.
            Dim localAll As Process() = Process.GetProcesses()

            ' Get all instances of Notepad running on the local computer.
            ' This will return an empty array if notepad isn't running.
            Dim localByName As Process() = 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.
            Dim localById As Process = 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.
            Dim remoteAll As Process() = Process.GetProcesses("myComputer")

            ' Get all instances of Notepad running on the specific computer, using machine name.
            Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")

            ' Get all instances of Notepad running on the specific computer, using IP address.
            Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")

            ' Get a process on a remote computer, using the process id and machine name.
            Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
        End Sub

        Shared Sub Main()
            Dim myProcess As New MyProcess()
            myProcess.BindToRunningProcesses()
        End Sub

    End Class

End Namespace 'MyProcessSample

Commenti

Utilizzare questo metodo per creare un nuovo Process componente e associarlo a una risorsa di processo nel computer locale. La risorsa di processo deve esistere già nel computer, perché GetProcessById(Int32) non crea una risorsa di sistema, ma associa una risorsa a un componente generato dall'applicazione Process . Un processo Id può essere recuperato solo per un processo attualmente in esecuzione nel computer. Al termine del processo, GetProcessById(Int32) genera un'eccezione se viene passato un identificatore scaduto.

In qualsiasi computer specifico, l'identificatore di un processo è univoco. GetProcessById(Int32) restituisce al massimo un processo. Per ottenere tutti i processi che eseguono una determinata applicazione, usare GetProcessesByName(String). Se esistono più processi nel computer che esegue l'applicazione specificata, GetProcessesByName(String) restituisce una matrice contenente tutti i processi associati. È possibile eseguire una query su ognuno di questi processi a sua volta per il relativo identificatore. L'identificatore del processo può essere visualizzato nel Processes pannello di Gestione attività di Windows. Nella PID colonna viene visualizzato l'identificatore del processo assegnato a un processo.

Il processId parametro è un Int32 (intero con segno a 32 bit), anche se l'API di Windows sottostante usa un DWORD (intero senza segno a 32 bit) per API simili. Questo è per ragioni storiche.

Vedi anche

Si applica a

GetProcessById(Int32, String)

Restituisce un nuovo componente Process, dato un identificatore di processo e il nome di un computer sulla rete.

public:
 static System::Diagnostics::Process ^ GetProcessById(int processId, System::String ^ machineName);
public static System.Diagnostics.Process GetProcessById (int processId, string machineName);
static member GetProcessById : int * string -> System.Diagnostics.Process
Public Shared Function GetProcessById (processId As Integer, machineName As String) As Process

Parametri

processId
Int32

Identificatore univoco nel sistema di una risorsa di processo.

machineName
String

Nome di un computer della rete.

Restituisce

Process

Componente Process associato a una risorsa di processo remota identificata dal parametro processId.

Eccezioni

Il processo specificato dal parametro processId non è in esecuzione. L'identificatore potrebbe essere scaduto.

-oppure-

La sintassi del parametro machineName non è valida. Il nome potrebbe avere lunghezza zero (0).

Il valore del parametro machineName è null.

Il processo non è stato avviato da questo oggetto.

Esempio

Nell'esempio seguente vengono recuperate le informazioni del processo corrente, i processi in esecuzione nel computer locale, tutte le istanze del Blocco note in esecuzione nel computer locale e un processo specifico nel computer locale. Recupera quindi le informazioni per gli stessi processi in un computer remoto.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
   // Get the current process.    
   Process^ currentProcess = Process::GetCurrentProcess();

   // Get all processes running on the local computer.
   array<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.
   array<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.
   array<Process^>^remoteAll = Process::GetProcesses("myComputer");

   // Get all instances of Notepad running on the specific computer, using machine name.
   array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );
   
   // Get all instances of Notepad running on the specific computer, using IP address.
   array<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" );
}
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();
        }
    }
}
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        Sub BindToRunningProcesses()
            ' Get the current process. You can use currentProcess from this point
            ' to access various properties and call methods to control the process.
            Dim currentProcess As Process = Process.GetCurrentProcess()

            ' Get all processes running on the local computer.
            Dim localAll As Process() = Process.GetProcesses()

            ' Get all instances of Notepad running on the local computer.
            ' This will return an empty array if notepad isn't running.
            Dim localByName As Process() = 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.
            Dim localById As Process = 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.
            Dim remoteAll As Process() = Process.GetProcesses("myComputer")

            ' Get all instances of Notepad running on the specific computer, using machine name.
            Dim remoteByName As Process() = Process.GetProcessesByName("notepad", "myComputer")

            ' Get all instances of Notepad running on the specific computer, using IP address.
            Dim ipByName As Process() = Process.GetProcessesByName("notepad", "169.0.0.0")

            ' Get a process on a remote computer, using the process id and machine name.
            Dim remoteById As Process = Process.GetProcessById(2345, "myComputer")
        End Sub

        Shared Sub Main()
            Dim myProcess As New MyProcess()
            myProcess.BindToRunningProcesses()
        End Sub

    End Class

End Namespace 'MyProcessSample

Commenti

Usare questo metodo per creare un nuovo Process componente e associarlo a una risorsa di processo in un computer remoto in rete. La risorsa di processo deve esistere già nel computer specificato, perché GetProcessById(Int32, String) non crea una risorsa di sistema, ma associa una risorsa a un componente generato dall'applicazione Process . Un processo Id può essere recuperato solo per un processo attualmente in esecuzione nel computer. Al termine del processo, GetProcessById(Int32, String) genera un'eccezione se viene passato un identificatore scaduto.

In qualsiasi computer specifico, l'identificatore di un processo è univoco. GetProcessById(Int32, String) restituisce al massimo un processo. Per ottenere tutti i processi che eseguono una determinata applicazione, usare GetProcessesByName(String). Se esistono più processi nel computer che esegue l'applicazione specificata, GetProcessesByName(String) restituisce una matrice contenente tutti i processi associati. È possibile eseguire una query su ognuno di questi processi a sua volta per il relativo identificatore. L'identificatore del processo può essere visualizzato nel Processes pannello di Gestione attività di Windows. Nella PID colonna viene visualizzato l'identificatore del processo assegnato a un processo.

Se non si specifica un machineNameoggetto , viene utilizzato il computer locale. In alternativa, è possibile specificare il computer locale impostando machineName il valore "." o su una stringa vuota ("").

Il processId parametro è un Int32 (intero con segno a 32 bit), anche se l'API di Windows sottostante usa un DWORD (intero senza segno a 32 bit) per API simili. Questo è per ragioni storiche.

Vedi anche

Si applica a