Process.GetProcessById Metod

Definition

Skapar en ny Process komponent och associerar den med den befintliga processresurs som du anger.

Överlagringar

Name Description
GetProcessById(Int32)

Returnerar en ny Process komponent, givet identifieraren för en process på den lokala datorn.

GetProcessById(Int32, String)

Returnerar en ny Process komponent med en processidentifierare och namnet på en dator i nätverket.

GetProcessById(Int32)

Källa:
Process.cs
Källa:
Process.cs
Källa:
Process.cs
Källa:
Process.cs
Källa:
Process.cs

Returnerar en ny Process komponent, givet identifieraren för en process på den lokala datorn.

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

Parametrar

processId
Int32

System-unik identifierare för en processresurs.

Returer

En Process komponent som är associerad med den lokala processresursen som identifieras av parametern processId .

Undantag

Den process som anges av parametern processId körs inte. Identifieraren kan ha upphört att gälla.

Exempel

I följande exempel hämtas information om den aktuella processen, processer som körs på den lokala datorn, alla instanser av Anteckningar som körs på den lokala datorn och en specifik process på den lokala datorn. Den hämtar sedan information för samma processer på en fjärrdator.

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();
        }
    }
}
open System.Diagnostics

// Get the current process.
let currentProcess = Process.GetCurrentProcess()

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

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

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

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

// Get a process on a remote computer, using the process id and machine name.
let remoteById = Process.GetProcessById(2345, "myComputer")
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

Kommentarer

Använd den här metoden för att skapa en ny Process komponent och associera den med en processresurs på den lokala datorn. Processresursen måste redan finnas på datorn eftersom GetProcessById(Int32) den inte skapar en systemresurs, utan i stället associerar en resurs med en programgenererad Process komponent. En process Id kan endast hämtas för en process som för närvarande körs på datorn. När processen har avslutats GetProcessById(Int32) genererar ett undantag om du skickar ett utgånget ID.

På en viss dator är identifieraren för en process unik. GetProcessById(Int32) returnerar högst en process. Om du vill hämta alla processer som kör ett visst program använder du GetProcessesByName(String). Om det finns flera processer på datorn som kör det angivna programmet GetProcessesByName(String) returnerar en matris som innehåller alla associerade processer. Du kan i sin tur fråga var och en av dessa processer om dess identifierare. Processidentifieraren kan visas i panelen Processes i Windows Aktivitetshanteraren. Kolumnen PID visar den processidentifierare som har tilldelats till en process.

Parametern processId är en Int32 (ett 32-bitars signerat heltal), även om det underliggande Windows-API:et använder en DWORD (ett osignerat 32-bitars heltal) för liknande API:er. Detta är av historiska skäl.

Se även

Gäller för

GetProcessById(Int32, String)

Källa:
Process.cs
Källa:
Process.cs
Källa:
Process.cs
Källa:
Process.cs
Källa:
Process.cs

Returnerar en ny Process komponent med en processidentifierare och namnet på en dator i nätverket.

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

Parametrar

processId
Int32

System-unik identifierare för en processresurs.

machineName
String

Namnet på en dator i nätverket.

Returer

En Process komponent som är associerad med en fjärrprocessresurs som identifieras av parametern processId .

Undantag

Den process som anges av parametern processId körs inte. Identifieraren kan ha upphört att gälla.

-eller-

Parametersyntaxen machineName är ogiltig. Namnet kan ha längd noll (0).

Parametern machineName är null.

Exempel

I följande exempel hämtas information om den aktuella processen, processer som körs på den lokala datorn, alla instanser av Anteckningar som körs på den lokala datorn och en specifik process på den lokala datorn. Den hämtar sedan information för samma processer på en fjärrdator.

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();
        }
    }
}
open System.Diagnostics

// Get the current process.
let currentProcess = Process.GetCurrentProcess()

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

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

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

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

// Get a process on a remote computer, using the process id and machine name.
let remoteById = Process.GetProcessById(2345, "myComputer")
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

Kommentarer

Använd den här metoden för att skapa en ny Process komponent och associera den med en processresurs på en fjärrdator i nätverket. Processresursen måste redan finnas på den angivna datorn eftersom GetProcessById(Int32, String) den inte skapar en systemresurs, utan i stället associerar en resurs med en programgenererad Process komponent. En process Id kan endast hämtas för en process som för närvarande körs på datorn. När processen har avslutats GetProcessById(Int32, String) genererar ett undantag om du skickar ett utgånget ID.

På en viss dator är identifieraren för en process unik. GetProcessById(Int32, String) returnerar högst en process. Om du vill hämta alla processer som kör ett visst program använder du GetProcessesByName(String). Om det finns flera processer på datorn som kör det angivna programmet GetProcessesByName(String) returnerar en matris som innehåller alla associerade processer. Du kan i sin tur fråga var och en av dessa processer om dess identifierare. Processidentifieraren kan visas i panelen Processes i Windows Aktivitetshanteraren. Kolumnen PID visar den processidentifierare som har tilldelats till en process.

Om du inte anger en machineNameanvänds den lokala datorn. Du kan också ange den lokala datorn genom att ange machineName värdet "." eller till en tom sträng ("").

Parametern processId är en Int32 (ett 32-bitars signerat heltal), även om det underliggande Windows-API:et använder en DWORD (ett osignerat 32-bitars heltal) för liknande API:er. Detta är av historiska skäl.

Se även

Gäller för