Process.GetProcessById Méthode

Définition

Crée un nouveau composant Process et l'associe à la ressource de processus existante que vous spécifiez.

Surcharges

GetProcessById(Int32)

Retourne un nouveau composant Process, en fonction de l'identificateur d'un processus sur l'ordinateur local.

GetProcessById(Int32, String)

Retourne un nouveau composant Process, en fonction d'un identificateur de processus et du nom d'un ordinateur du réseau.

GetProcessById(Int32)

Retourne un nouveau composant Process, en fonction de l'identificateur d'un processus sur l'ordinateur local.

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

Paramètres

processId
Int32

Identificateur unique au système d'une ressource de processus.

Retours

Process

Composant Process associé à la ressource de processus locale identifiée par le paramètre processId.

Exceptions

Le processus spécifié par le paramètre processId n’est pas en cours d’exécution. L’identificateur peut avoir expiré.

Le processus n’a pas été démarré par cet objet.

Exemples

L’exemple suivant récupère les informations du processus actuel, les processus s’exécutant sur l’ordinateur local, toutes les instances du Bloc-notes s’exécutant sur l’ordinateur local et un processus spécifique sur l’ordinateur local. Il récupère ensuite des informations pour les mêmes processus sur un ordinateur distant.

#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

Remarques

Utilisez cette méthode pour créer un composant Process et l’associer à une ressource de processus sur l’ordinateur local. La ressource de processus doit déjà exister sur l’ordinateur, car GetProcessById(Int32) elle ne crée pas de ressource système, mais associe plutôt une ressource à un composant généré par Process l’application. Un processus Id ne peut être récupéré que pour un processus en cours d’exécution sur l’ordinateur. Une fois le processus terminé, GetProcessById(Int32) lève une exception si vous lui transmettez un identificateur expiré.

Sur un ordinateur particulier, l’identificateur d’un processus est unique. GetProcessById(Int32) retourne un processus au maximum. Si vous souhaitez obtenir tous les processus exécutant une application particulière, utilisez GetProcessesByName(String). Si plusieurs processus existent sur l’ordinateur exécutant l’application spécifiée, GetProcessesByName(String) retourne un tableau contenant tous les processus associés. Vous pouvez interroger chacun de ces processus à son tour pour son identificateur. L’identificateur de processus peut être consulté dans le Processes panneau du Gestionnaire des tâches Windows. La PID colonne affiche l’identificateur de processus affecté à un processus.

Le processId paramètre est un Int32 (entier signé 32 bits), bien que l’API Windows sous-jacente utilise un DWORD (entier 32 bits non signé) pour des API similaires. C’est pour des raisons historiques.

Voir aussi

S’applique à

GetProcessById(Int32, String)

Retourne un nouveau composant Process, en fonction d'un identificateur de processus et du nom d'un ordinateur du réseau.

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

Paramètres

processId
Int32

Identificateur unique au système d'une ressource de processus.

machineName
String

Nom d'un ordinateur du réseau.

Retours

Process

Composant Process associé à une ressource de processus distante identifiée par le paramètre processId.

Exceptions

Le processus spécifié par le paramètre processId n’est pas en cours d’exécution. L’identificateur peut avoir expiré.

  • ou -

La syntaxe du paramètre machineName n’est pas valide. Le nom peut être de longueur zéro (0).

Le paramètre machineName a la valeur null.

Le processus n’a pas été démarré par cet objet.

Exemples

L’exemple suivant récupère les informations du processus actuel, les processus s’exécutant sur l’ordinateur local, toutes les instances du Bloc-notes s’exécutant sur l’ordinateur local et un processus spécifique sur l’ordinateur local. Il récupère ensuite des informations pour les mêmes processus sur un ordinateur distant.

#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

Remarques

Utilisez cette méthode pour créer un composant Process et l’associer à une ressource de processus sur un ordinateur distant sur le réseau. La ressource de processus doit déjà exister sur l’ordinateur spécifié, car GetProcessById(Int32, String) elle ne crée pas de ressource système, mais associe plutôt une ressource à un composant généré par Process l’application. Un processus Id ne peut être récupéré que pour un processus en cours d’exécution sur l’ordinateur. Une fois le processus terminé, GetProcessById(Int32, String) lève une exception si vous lui transmettez un identificateur expiré.

Sur un ordinateur particulier, l’identificateur d’un processus est unique. GetProcessById(Int32, String) retourne un processus au maximum. Si vous souhaitez obtenir tous les processus exécutant une application particulière, utilisez GetProcessesByName(String). Si plusieurs processus existent sur l’ordinateur exécutant l’application spécifiée, GetProcessesByName(String) retourne un tableau contenant tous les processus associés. Vous pouvez interroger chacun de ces processus à son tour pour son identificateur. L’identificateur de processus peut être consulté dans le Processes panneau du Gestionnaire des tâches Windows. La PID colonne affiche l’identificateur de processus affecté à un processus.

Si vous ne spécifiez pas un machineName, l’ordinateur local est utilisé. Vous pouvez également spécifier l’ordinateur local en définissant machineName la valeur « ». ou sur une chaîne vide ( » « ).

Le processId paramètre est un Int32 (entier signé 32 bits), bien que l’API Windows sous-jacente utilise un DWORD (entier 32 bits non signé) pour des API similaires. C’est pour des raisons historiques.

Voir aussi

S’applique à