Process.GetProcessById Metoda

Definicja

Tworzy nowy Process składnik i kojarzy go z określonym zasobem procesu.

Przeciążenia

GetProcessById(Int32)

Zwraca nowy Process składnik, biorąc pod uwagę identyfikator procesu na komputerze lokalnym.

GetProcessById(Int32, String)

Zwraca nowy Process składnik z identyfikatorem procesu i nazwą komputera w sieci.

GetProcessById(Int32)

Źródło:
Process.cs
Źródło:
Process.cs
Źródło:
Process.cs

Zwraca nowy Process składnik, biorąc pod uwagę identyfikator procesu na komputerze lokalnym.

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

Parametry

processId
Int32

Unikatowy identyfikator systemu zasobu procesu.

Zwraca

Process Składnik skojarzony z zasobem procesu lokalnego processId zidentyfikowany przez parametr .

Wyjątki

Proces określony przez processId parametr nie jest uruchomiony. Identyfikator może zostać wygasł.

Przykłady

Poniższy przykład pobiera informacje o bieżącym procesie, procesy uruchomione na komputerze lokalnym, wszystkie wystąpienia Notatnika uruchomione na komputerze lokalnym i określony proces na komputerze lokalnym. Następnie pobiera informacje o tych samych procesach na komputerze zdalnym.

#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

Uwagi

Użyj tej metody, aby utworzyć nowy Process składnik i skojarzyć go z zasobem procesu na komputerze lokalnym. Zasób procesu musi już istnieć na komputerze, ponieważ GetProcessById(Int32) nie tworzy zasobu systemowego, ale kojarzy zasób z składnikiem wygenerowanym Process przez aplikację. Id Proces można pobrać tylko dla procesu, który jest obecnie uruchomiony na komputerze. Po zakończeniu GetProcessById(Int32) procesu zgłasza wyjątek po przekazaniu wygasłego identyfikatora.

Na dowolnym konkretnym komputerze identyfikator procesu jest unikatowy. GetProcessById(Int32) zwraca co najwyżej jeden proces. Jeśli chcesz pobrać wszystkie procesy z określoną aplikacją, użyj polecenia GetProcessesByName(String). Jeśli na komputerze z uruchomioną określoną aplikacją istnieje wiele procesów, GetProcessesByName(String) zwraca tablicę zawierającą wszystkie skojarzone procesy. Dla jego identyfikatora można wykonywać zapytania dotyczące każdego z tych procesów. Identyfikator procesu można wyświetlić w Processes panelu Menedżera zadań systemu Windows. W PID kolumnie zostanie wyświetlony identyfikator procesu przypisany do procesu.

Parametr processId jest Int32 (32-bitową liczbą całkowitą z podpisem), chociaż podstawowy interfejs API systemu Windows używa DWORD (niepodpisanej liczby całkowitej 32-bitowej) dla podobnych interfejsów API. Jest to ze względów historycznych.

Zobacz też

Dotyczy

GetProcessById(Int32, String)

Źródło:
Process.cs
Źródło:
Process.cs
Źródło:
Process.cs

Zwraca nowy Process składnik z identyfikatorem procesu i nazwą komputera w sieci.

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

Parametry

processId
Int32

Unikatowy identyfikator systemu zasobu procesu.

machineName
String

Nazwa komputera w sieci.

Zwraca

Process Składnik skojarzony z zasobem procesu zdalnego zidentyfikowany przez processId parametr .

Wyjątki

Proces określony przez processId parametr nie jest uruchomiony. Identyfikator może zostać wygasł.

-lub-

Składnia parametru jest nieprawidłowa machineName . Nazwa może mieć długość zero (0).

Parametr machineName to null.

Przykłady

Poniższy przykład pobiera informacje o bieżącym procesie, procesy uruchomione na komputerze lokalnym, wszystkie wystąpienia Notatnika uruchomione na komputerze lokalnym i określony proces na komputerze lokalnym. Następnie pobiera informacje o tych samych procesach na komputerze zdalnym.

#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

Uwagi

Użyj tej metody, aby utworzyć nowy Process składnik i skojarzyć go z zasobem procesu na komputerze zdalnym w sieci. Zasób procesu musi już istnieć na określonym komputerze, ponieważ GetProcessById(Int32, String) nie tworzy zasobu systemowego, ale raczej kojarzy zasób z składnikiem wygenerowanym Process przez aplikację. Id Proces można pobrać tylko dla procesu, który jest obecnie uruchomiony na komputerze. Po zakończeniu GetProcessById(Int32, String) procesu zgłasza wyjątek po przekazaniu wygasłego identyfikatora.

Na dowolnym konkretnym komputerze identyfikator procesu jest unikatowy. GetProcessById(Int32, String) zwraca co najwyżej jeden proces. Jeśli chcesz pobrać wszystkie procesy z określoną aplikacją, użyj polecenia GetProcessesByName(String). Jeśli na komputerze z uruchomioną określoną aplikacją istnieje wiele procesów, GetProcessesByName(String) zwraca tablicę zawierającą wszystkie skojarzone procesy. Dla jego identyfikatora można wykonywać zapytania dotyczące każdego z tych procesów. Identyfikator procesu można wyświetlić w Processes panelu Menedżera zadań systemu Windows. W PID kolumnie zostanie wyświetlony identyfikator procesu przypisany do procesu.

Jeśli nie określisz elementu machineName, używany jest komputer lokalny. Możesz też określić komputer lokalny, ustawiając machineName wartość "." lub na pusty ciąg ("").

Parametr processId jest Int32 (32-bitową liczbą całkowitą z podpisem), chociaż podstawowy interfejs API systemu Windows używa DWORD (niepodpisanej liczby całkowitej 32-bitowej) dla podobnych interfejsów API. Jest to ze względów historycznych.

Zobacz też

Dotyczy