Process.GetProcessesByName Metoda

Definice

Vytvoří pole nových Process komponent a přidruží je k existujícím prostředkům procesu, které všechny sdílejí zadaný název procesu.

Přetížení

Name Description
GetProcessesByName(String)

Vytvoří pole nových komponent Process a přidruží je ke všem prostředkům procesu v místním počítači, který sdílí zadaný název procesu.

GetProcessesByName(String, String)

Vytvoří pole nových komponent Process a přidruží je ke všem prostředkům procesu ve vzdáleném počítači, který sdílí zadaný název procesu.

GetProcessesByName(String)

Zdroj:
Process.cs
Zdroj:
Process.cs
Zdroj:
Process.cs
Zdroj:
Process.cs
Zdroj:
Process.cs

Vytvoří pole nových komponent Process a přidruží je ke všem prostředkům procesu v místním počítači, který sdílí zadaný název procesu.

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName);
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process[] GetProcessesByName(string? processName);
public static System.Diagnostics.Process[] GetProcessesByName(string? processName);
public static System.Diagnostics.Process[] GetProcessesByName(string processName);
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member GetProcessesByName : string -> System.Diagnostics.Process[]
static member GetProcessesByName : string -> System.Diagnostics.Process[]
Public Shared Function GetProcessesByName (processName As String) As Process()

Parametry

processName
String

Popisný název procesu.

Návraty

Pole typu Process , které představuje procesní prostředky, na kterých běží zadaná aplikace nebo soubor.

Atributy

Výjimky

Při přístupu k rozhraním API čítačů výkonu, která se používají k získání informací o procesu, dochází k problémům. Tato výjimka je specifická pro Windows NT, Windows 2000 a Windows XP.

Příklady

Následující příklad načte informace o aktuálním procesu, procesy spuštěné v místním počítači, všechny instance Poznámkového bloku spuštěné v místním počítači a konkrétní proces na místním počítači. Potom načte informace pro stejné procesy ve vzdáleném počítači.

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

Poznámky

Pomocí této metody vytvořte pole nových Process komponent a přidružte je ke všem prostředkům procesu, na kterých běží stejný spustitelný soubor v místním počítači. Prostředky procesu již musí existovat v počítači, protože GetProcessesByName nevytvoří systémové prostředky, ale spíše je přidruží k komponentám generovaným Process aplikacím. Lze processName zadat pro spustitelný soubor, který aktuálně není spuštěn v místním počítači, takže pole, které metoda vrací, může být prázdné.

Název procesu je popisný název procesu, například Outlook, který neobsahuje rozšíření .exe ani cestu. GetProcessesByName je užitečné pro získání a manipulaci se všemi procesy, které jsou přidružené ke stejnému spustitelnému souboru. Jako parametr můžete například předat název processName spustitelného souboru, abyste vypnuli všechny spuštěné instance tohoto spustitelného souboru.

I když je proces Id jedinečný pro jeden prostředek procesu v systému, více procesů v místním počítači může být spuštěna aplikace určená parametrem processName . GetProcessById Proto vrátí maximálně jeden proces, ale GetProcessesByName vrátí pole obsahující všechny přidružené procesy. Pokud potřebujete manipulovat s procesem pomocí standardních volání rozhraní API, můžete všechny tyto procesy dotazovat na jeho identifikátor. K prostředkům procesu nelze přistupovat pouze prostřednictvím názvu procesu, ale jakmile načtete pole komponent přidružených Process k prostředkům procesu, můžete spustit, ukončit a jinak manipulovat se systémovými prostředky.

Viz také

Platí pro

GetProcessesByName(String, String)

Zdroj:
Process.Linux.cs
Zdroj:
Process.cs
Zdroj:
Process.Linux.cs
Zdroj:
Process.Linux.cs
Zdroj:
Process.Linux.cs

Vytvoří pole nových komponent Process a přidruží je ke všem prostředkům procesu ve vzdáleném počítači, který sdílí zadaný název procesu.

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName, System::String ^ machineName);
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static System.Diagnostics.Process[] GetProcessesByName(string? processName, string machineName);
public static System.Diagnostics.Process[] GetProcessesByName(string? processName, string machineName);
public static System.Diagnostics.Process[] GetProcessesByName(string processName, string machineName);
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member GetProcessesByName : string * string -> System.Diagnostics.Process[]
static member GetProcessesByName : string * string -> System.Diagnostics.Process[]
Public Shared Function GetProcessesByName (processName As String, machineName As String) As Process()

Parametry

processName
String

Popisný název procesu.

machineName
String

Název počítače v síti.

Návraty

Pole typu Process , které představuje procesní prostředky, na kterých běží zadaná aplikace nebo soubor.

Atributy

Výjimky

machineName Syntaxe parametru je neplatná. Může mít délku nula (0).

Parametr machineName je null.

Platforma operačního systému tuto operaci na vzdálených počítačích nepodporuje.

Pokus o připojení machineName se nezdařil.

nebo

Při přístupu k rozhraním API čítačů výkonu, která se používají k získání informací o procesu, dochází k problémům. Tato výjimka je specifická pro Windows NT, Windows 2000 a Windows XP.

Při přístupu k základnímu systémovému rozhraní API došlo k problému.

Příklady

Následující příklad načte informace o aktuálním procesu, procesy spuštěné v místním počítači, všechny instance Poznámkového bloku spuštěné v místním počítači a konkrétní proces na místním počítači. Potom načte informace pro stejné procesy ve vzdáleném počítači.

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

Poznámky

Pomocí této metody vytvořte pole nových Process komponent a přidružte je ke všem prostředkům procesu, na kterých běží stejný spustitelný soubor v zadaném počítači. Prostředky procesu již musí existovat v počítači, protože GetProcessesByName nevytvoří systémové prostředky, ale spíše je přidruží k komponentám generovaným Process aplikacím. Lze processName zadat pro spustitelný soubor, který aktuálně není spuštěn v místním počítači, takže pole, které metoda vrací, může být prázdné.

Název procesu je popisný název procesu, například Outlook, který neobsahuje rozšíření .exe ani cestu. GetProcessesByName je užitečné pro získání a manipulaci se všemi procesy, které jsou přidružené ke stejnému spustitelnému souboru. Jako parametr můžete například předat název processName spustitelného souboru, abyste vypnuli všechny spuštěné instance tohoto spustitelného souboru.

I když je proces Id jedinečný pro jeden prostředek procesu v systému, více procesů v místním počítači může být spuštěna aplikace určená parametrem processName . GetProcessById Proto vrátí maximálně jeden proces, ale GetProcessesByName vrátí pole obsahující všechny přidružené procesy. Pokud potřebujete manipulovat s procesem pomocí standardních volání rozhraní API, můžete všechny tyto procesy dotazovat na jeho identifikátor. K prostředkům procesu nelze přistupovat pouze prostřednictvím názvu procesu, ale jakmile načtete pole komponent přidružených Process k prostředkům procesu, můžete spustit, ukončit a jinak manipulovat se systémovými prostředky.

Toto přetížení můžete použít k získání procesů na místním počítači i na vzdáleném počítači. K určení místního počítače použijte "." Existuje další přetížení, které ve výchozím nastavení používá místní počítač.

K procesům na vzdálených počítačích můžete přistupovat pouze k zobrazení informací, jako jsou statistiky, o procesech. Na vzdálených počítačích nelze ukončit, ukončit (pomocí Kill) ani spustit procesy.

Viz také

Platí pro