Process.GetProcessesByName 方法

定义

创建新的 Process 组件的数组,并将它们与共享指定的进程名称的所有现有进程资源关联。

重载

GetProcessesByName(String, String)

创建新的 Process 组件的数组,并将它们与远程计算机上共享指定进程名称的所有进程资源关联。

GetProcessesByName(String)

创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。

GetProcessesByName(String, String)

Source:
Process.Linux.cs
Source:
Process.Linux.cs
Source:
Process.Linux.cs

创建新的 Process 组件的数组,并将它们与远程计算机上共享指定进程名称的所有进程资源关联。

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

参数

processName
String

该进程的友好名称。

machineName
String

网络上计算机的名称。

返回

类型 Process 的数组,表示运行指定应用程序或文件的进程资源。

属性

例外

machineName 参数的语法无效。 其长度可能为零 (0)。

machineName 参数为 null

操作系统平台不支持在远程计算机上进行此操作。

尝试连接到 machineName 失败。

访问用于获取进程信息的性能计数器 API 时遇到问题。 此异常特定于 Windows NT、Windows 2000 和 Windows XP。

访问基础系统 API 时出现问题。

示例

以下示例检索当前进程、在本地计算机上运行的进程、在本地计算机上运行的所有记事本实例以及本地计算机上的特定进程的信息。 然后,它会检索远程计算机上相同进程的信息。

#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 组件构成的数组,并将其与在指定计算机上运行相同可执行文件的所有进程资源相关联。 进程资源必须已存在于计算机上,因为 GetProcessesByName 不会创建系统资源,而是将其与应用程序生成的 Process 组件相关联。 processName可以为当前未在本地计算机上运行的可执行文件指定 ,因此该方法返回的数组可以为空。

进程名称是进程(如 Outlook)的友好名称,不包括 .exe 扩展或路径。 GetProcessesByName 有助于获取和操作与同一可执行文件关联的所有进程。 例如,可以传递可执行文件名称作为 processName 参数,以关闭该可执行文件的所有正在运行的实例。

尽管进程 Id 对于系统上的单个进程资源是唯一的,但本地计算机上的多个进程可以运行 参数 processName 指定的应用程序。 因此, GetProcessById 最多返回一个进程,但 GetProcessesByName 返回包含所有关联进程的数组。 如果需要使用标准 API 调用操作进程,可以依次查询其中每个进程的标识符。 不能单独通过进程名称访问进程资源,但检索到已与进程资源关联的组件数组 Process 后,可以启动、终止和以其他方式操作系统资源。

可以使用此重载在本地计算机和远程计算机上获取进程。 使用“.”指定本地计算机。 存在另一个默认使用本地计算机的重载。

只能访问远程计算机上的进程来查看有关进程的信息(例如统计信息)。 无法使用) 关闭、终止 (Kill 或启动远程计算机上的进程。

另请参阅

适用于

GetProcessesByName(String)

Source:
Process.cs
Source:
Process.cs
Source:
Process.cs

创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。

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

参数

processName
String

该进程的友好名称。

返回

类型 Process 的数组,表示运行指定应用程序或文件的进程资源。

属性

例外

访问用于获取进程信息的性能计数器 API 时遇到问题。 此异常特定于 Windows NT、Windows 2000 和 Windows XP。

示例

以下示例检索当前进程、在本地计算机上运行的进程、在本地计算机上运行的所有记事本实例以及本地计算机上的特定进程的信息。 然后,它会检索远程计算机上相同进程的信息。

#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 组件构成的数组,并将其与在本地计算机上运行同一可执行文件的所有进程资源相关联。 进程资源必须已存在于计算机上,因为 GetProcessesByName 不会创建系统资源,而是将其与应用程序生成的 Process 组件相关联。 processName可以为当前未在本地计算机上运行的可执行文件指定 ,因此该方法返回的数组可以为空。

进程名称是进程(如 Outlook)的友好名称,不包括 .exe 扩展或路径。 GetProcessesByName 有助于获取和操作与同一可执行文件关联的所有进程。 例如,可以传递可执行文件名称作为 processName 参数,以关闭该可执行文件的所有正在运行的实例。

尽管进程 Id 对于系统上的单个进程资源是唯一的,但本地计算机上的多个进程可以运行 参数 processName 指定的应用程序。 因此, GetProcessById 最多返回一个进程,但 GetProcessesByName 返回包含所有关联进程的数组。 如果需要使用标准 API 调用操作进程,可以依次查询其中每个进程的标识符。 不能单独通过进程名称访问进程资源,但检索到已与进程资源关联的组件数组 Process 后,可以启动、终止和以其他方式操作系统资源。

另请参阅

适用于