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, biorąc pod uwagę identyfikator 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 dotyczące tych samych procesów 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

Ta metoda umożliwia utworzenie nowego Process składnika i skojarzenie 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ę. Proces Id można pobrać tylko dla procesu, który jest obecnie uruchomiony na komputerze. Po zakończeniu procesu zgłasza wyjątek w GetProcessById(Int32) przypadku przekazania wygasłego identyfikatora.

Na dowolnym konkretnym komputerze identyfikator procesu jest unikatowy. GetProcessById(Int32) metoda 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. Możesz wykonać zapytanie dotyczące każdego z tych procesów z kolei pod kątem jego identyfikatora. 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ą ze znakiem), chociaż podstawowy interfejs API systemu Windows używa DWORD (niepodpisanej liczby całkowitej 32-bitowej) dla podobnych interfejsów API. Jest to z powodó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, biorąc pod uwagę identyfikator 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 identyfikowany 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 ma wartość 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 dotyczące tych samych procesów 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

Ta metoda umożliwia utworzenie nowego Process składnika i skojarzenie 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 kojarzy zasób z składnikiem wygenerowanym Process przez aplikację. Proces Id można pobrać tylko dla procesu, który jest obecnie uruchomiony na komputerze. Po zakończeniu procesu zgłasza wyjątek w GetProcessById(Int32, String) przypadku przekazania wygasłego identyfikatora.

Na dowolnym konkretnym komputerze identyfikator procesu jest unikatowy. GetProcessById(Int32, String) metoda 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. Możesz wykonać zapytanie dotyczące każdego z tych procesów z kolei pod kątem jego identyfikatora. 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 machineNameelementu , używany jest komputer lokalny. Alternatywnie możesz określić komputer lokalny, ustawiając machineName wartość "." lub na pusty ciąg ("").

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

Zobacz też

Dotyczy