Process.GetProcessesByName 方法

定义

创建新的 Process 组件的数组,并将它们与共享指定的进程名称的所有现有进程资源关联。Creates an array of new Process components and associates them with the existing process resources that all share the specified process name.

重载

GetProcessesByName(String, String)

创建新的 Process 组件的数组,并将它们与远程计算机上共享指定进程名称的所有进程资源关联。Creates an array of new Process components and associates them with all the process resources on a remote computer that share the specified process name.

GetProcessesByName(String)

创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。Creates an array of new Process components and associates them with all the process resources on the local computer that share the specified process name.

GetProcessesByName(String, String)

创建新的 Process 组件的数组,并将它们与远程计算机上共享指定进程名称的所有进程资源关联。Creates an array of new Process components and associates them with all the process resources on a remote computer that share the specified process name.

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName, System::String ^ machineName);
public static System.Diagnostics.Process[] GetProcessesByName (string? processName, string machineName);
public static System.Diagnostics.Process[] GetProcessesByName (string processName, string machineName);
static member GetProcessesByName : string * string -> System.Diagnostics.Process[]
Public Shared Function GetProcessesByName (processName As String, machineName As String) As Process()

参数

processName
String

该进程的友好名称。The friendly name of the process.

machineName
String

网络上计算机的名称。The name of a computer on the network.

返回

Process[]

类型 Process 的数组,表示运行指定应用程序或文件的进程资源。An array of type Process that represents the process resources running the specified application or file.

例外

machineName 参数的语法无效。The machineName parameter syntax is invalid. 其长度可能为零 (0)。It might have length zero (0).

machineName 参数为 nullThe machineName parameter is null.

操作系统平台不支持在远程计算机上进行此操作。The operating system platform does not support this operation on remote computers.

尝试连接到 machineName 失败。The attempt to connect to machineName has failed.

-or- 访问用于获取进程信息的性能计数器 API 时遇到问题。There are problems accessing the performance counter APIs used to get process information. 此异常特定于 Windows NT、Windows 2000 和 Windows XP。This exception is specific to Windows NT, Windows 2000, and Windows XP.

访问基础系统 API 时出现问题。A problem occurred accessing an underlying system API.

示例

下面的示例检索当前进程的信息、在本地计算机上运行的进程、在本地计算机上运行的所有 Notepad 实例,以及本地计算机上的特定进程。The following example retrieves information of the current process, processes running on the local computer, all instances of Notepad running on the local computer, and a specific process on the local computer. 然后,它将检索远程计算机上相同进程的信息。It then retrieves information for the same processes on a remote computer.

#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

注解

使用此方法创建新的组件的数组 Process ,并将其与在指定计算机上运行相同可执行文件的所有进程资源关联。Use this method to create an array of new Process components and associate them with all the process resources that are running the same executable file on the specified computer. 进程资源必须已存在于计算机上,因为不 GetProcessesByName 会创建系统资源,而是将它们与应用程序生成的组件相关联 ProcessThe process resources must already exist on the computer, because GetProcessesByName does not create system resources but rather associates them with application-generated Process components. processName可以为当前未在本地计算机上运行的可执行文件指定,因此该方法返回的数组可以为空。A processName can be specified for an executable file that is not currently running on the local computer, so the array the method returns can be empty.

进程名称是进程(例如 Outlook)的友好名称,该名称不包括 .exe 扩展名或路径。The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path. GetProcessesByName 对于获取和操作与同一可执行文件关联的所有进程很有用。GetProcessesByName is helpful for getting and manipulating all the processes that are associated with the same executable file. 例如,可以将可执行文件名称作为 processName 参数进行传递,以便关闭该可执行文件的所有正在运行的实例。For example, you can pass an executable file name as the processName parameter, in order to shut down all the running instances of that executable file.

尽管进程 Id 对于系统上的单个进程资源是唯一的,但本地计算机上的多个进程可以运行参数指定的应用程序 processNameAlthough a process Id is unique to a single process resource on the system, multiple processes on the local computer can be running the application specified by the processName parameter. 因此, GetProcessById 最多返回一个进程,但 GetProcessesByName 返回包含所有关联进程的数组。Therefore, GetProcessById returns one process at most, but GetProcessesByName returns an array containing all the associated processes. 如果需要使用标准 API 调用来操作进程,可以依次查询每个进程的标识符。If you need to manipulate the process using standard API calls, you can query each of these processes in turn for its identifier. 你不能只通过进程名称访问进程资源,但一旦检索到 Process 与进程资源关联的组件的数组,就可以启动、终止和其他操作系统资源。You cannot access process resources through the process name alone but, once you have retrieved an array of Process components that have been associated with the process resources, you can start, terminate, and otherwise manipulate the system resources.

您可以使用此重载获取本地计算机以及远程计算机上的进程。You can use this overload to get processes on the local computer as well as on a remote computer. 使用 "." 指定本地计算机。Use "." to specify the local computer. 默认情况下,存在使用本地计算机的另一个重载。Another overload exists that uses the local computer by default.

您可以访问远程计算机上的进程,以便查看有关进程的信息(例如统计信息)。You can access processes on remote computers only to view information, such as statistics, about the processes. 您无法关闭、终止使用) 的 (Kill ,或在远程计算机上启动进程。You cannot close, terminate (using Kill), or start processes on remote computers.

另请参阅

适用于

GetProcessesByName(String)

创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。Creates an array of new Process components and associates them with all the process resources on the local computer that share the specified process name.

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcessesByName(System::String ^ processName);
public static System.Diagnostics.Process[] GetProcessesByName (string? processName);
public static System.Diagnostics.Process[] GetProcessesByName (string processName);
static member GetProcessesByName : string -> System.Diagnostics.Process[]
Public Shared Function GetProcessesByName (processName As String) As Process()

参数

processName
String

该进程的友好名称。The friendly name of the process.

返回

Process[]

类型 Process 的数组,表示运行指定应用程序或文件的进程资源。An array of type Process that represents the process resources running the specified application or file.

例外

访问用于获取进程信息的性能计数器 API 时遇到问题。There are problems accessing the performance counter APIs used to get process information. 此异常特定于 Windows NT、Windows 2000 和 Windows XP。This exception is specific to Windows NT, Windows 2000, and Windows XP.

示例

下面的示例检索当前进程的信息、在本地计算机上运行的进程、在本地计算机上运行的所有 Notepad 实例,以及本地计算机上的特定进程。The following example retrieves information of the current process, processes running on the local computer, all instances of Notepad running on the local computer, and a specific process on the local computer. 然后,它将检索远程计算机上相同进程的信息。It then retrieves information for the same processes on a remote computer.

#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

注解

使用此方法创建新的组件的数组 Process ,并将它们与本地计算机上运行相同可执行文件的所有进程资源关联。Use this method to create an array of new Process components and associate them with all the process resources that are running the same executable file on the local computer. 进程资源必须已存在于计算机上,因为不 GetProcessesByName 会创建系统资源,而是将它们与应用程序生成的组件相关联 ProcessThe process resources must already exist on the computer, because GetProcessesByName does not create system resources but rather associates them with application-generated Process components. processName可以为当前未在本地计算机上运行的可执行文件指定,因此该方法返回的数组可以为空。A processName can be specified for an executable file that is not currently running on the local computer, so the array the method returns can be empty.

进程名称是进程(例如 Outlook)的友好名称,该名称不包括 .exe 扩展名或路径。The process name is a friendly name for the process, such as Outlook, that does not include the .exe extension or the path. GetProcessesByName 对于获取和操作与同一可执行文件关联的所有进程很有用。GetProcessesByName is helpful for getting and manipulating all the processes that are associated with the same executable file. 例如,可以将可执行文件名称作为 processName 参数进行传递,以便关闭该可执行文件的所有正在运行的实例。For example, you can pass an executable file name as the processName parameter, in order to shut down all the running instances of that executable file.

尽管进程 Id 对于系统上的单个进程资源是唯一的,但本地计算机上的多个进程可以运行参数指定的应用程序 processNameAlthough a process Id is unique to a single process resource on the system, multiple processes on the local computer can be running the application specified by the processName parameter. 因此, GetProcessById 最多返回一个进程,但 GetProcessesByName 返回包含所有关联进程的数组。Therefore, GetProcessById returns one process at most, but GetProcessesByName returns an array containing all the associated processes. 如果需要使用标准 API 调用来操作进程,可以依次查询每个进程的标识符。If you need to manipulate the process using standard API calls, you can query each of these processes in turn for its identifier. 你不能只通过进程名称访问进程资源,但一旦检索到 Process 与进程资源关联的组件的数组,就可以启动、终止和其他操作系统资源。You cannot access process resources through the process name alone but, once you have retrieved an array of Process components that have been associated with the process resources, you can start, terminate, and otherwise manipulate the system resources.

另请参阅

适用于