Process.GetProcesses 方法

定義

建立新 Process 元件的陣列,並將其與現有的處理序資源相關聯。Creates an array of new Process components and associates them with existing process resources.

多載

GetProcesses()

為本機電腦上的每個處理序資源建立新的 Process 元件。Creates a new Process component for each process resource on the local computer.

GetProcesses(String)

為指定電腦上的每個處理序資源建立新的 Process 元件。Creates a new Process component for each process resource on the specified computer.

GetProcesses()

為本機電腦上的每個處理序資源建立新的 Process 元件。Creates a new Process component for each process resource on the local computer.

public:
 static cli::array <System::Diagnostics::Process ^> ^ GetProcesses();
public static System.Diagnostics.Process[] GetProcesses ();
static member GetProcesses : unit -> System.Diagnostics.Process[]
Public Shared Function GetProcesses () As Process()

傳回

Process[]

Process 類型的陣列,代表正在本機電腦上執行的所有處理序資源。An array of type Process that represents all the process resources running on the local computer.

範例

下列範例會抓取目前進程的資訊、在本機電腦上執行的進程、在本機電腦上執行的所有 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 on the local computer. 進程資源必須已經存在於本機電腦上,因為不 GetProcesses 會建立系統資源,而是將資源與應用程式產生的元件產生關聯 ProcessThe process resources must already exist on the local computer, because GetProcesses does not create system resources but rather associates resources with application-generated Process components. 因為作業系統本身正在執行背景進程,所以這個陣列永遠不會是空的。Because the operating system itself is running background processes, this array is never empty.

如果您不想要取得電腦上執行的所有處理常式,您可以使用或方法來限制其數目 GetProcessById GetProcessesByNameIf you do not want to retrieve all the processes running on the computer, you can restrict their number by using the GetProcessById or GetProcessesByName method. GetProcessByIdProcess使用您傳遞給方法的進程識別碼,建立與系統上識別的進程相關聯的元件。GetProcessById creates a Process component that is associated with the process identified on the system by the process identifier that you pass to the method. GetProcessesByName 建立元件的陣列, Process 其相關聯的進程資源會共用您傳遞給方法的可執行檔。GetProcessesByName creates an array of Process components whose associated process resources share the executable file you pass to the method.

注意

多個 Windows 服務可以在服務主機進程的相同實例中載入 (svchost.exe) 。Multiple Windows services can be loaded within the same instance of the Service Host process (svchost.exe). System.diagnostics.process.getprocesses 不會識別這些個別服務;若要這樣做,請參閱 GetServicesGetProcesses does not identify those individual services; for that, see GetServices.

另請參閱

適用於

GetProcesses(String)

為指定電腦上的每個處理序資源建立新的 Process 元件。Creates a new Process component for each process resource on the specified computer.

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

參數

machineName
String

要讀取處理序清單的電腦。The computer from which to read the list of processes.

傳回

Process[]

Process 類型的陣列,代表正在指定的電腦上執行的所有處理序資源。An array of type Process that represents all the process resources running on the specified computer.

例外狀況

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.

存取用來取得處理序資訊的效能計數器 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 on the specified (usually remote) computer. 進程資源必須已經存在於本機電腦上,因為不 GetProcesses 會建立系統資源,而是將資源與應用程式產生的元件產生關聯 ProcessThe process resources must already exist on the local computer, because GetProcesses does not create system resources but rather associates resources with application-generated Process components. 因為作業系統本身正在執行背景進程,所以這個陣列永遠不會是空的。Because the operating system itself is running background processes, this array is never empty.

如果您不想要取得電腦上執行的所有處理常式,您可以使用或方法來限制其數目 GetProcessById GetProcessesByNameIf you do not want to retrieve all the processes running on the computer, you can restrict their number by using the GetProcessById or GetProcessesByName method. GetProcessByIdProcess使用您傳遞給方法的進程識別碼,建立與系統上識別的進程相關聯的元件。GetProcessById creates a Process component that is associated with the process identified on the system by the process identifier that you pass to the method. GetProcessesByName 建立元件的陣列, Process 其相關聯的進程資源會共用您傳遞給方法的可執行檔。GetProcessesByName creates an array of Process components whose associated process resources share the executable file you pass to the method.

方法的這個多載 GetProcesses 通常用來取出在網路上的遠端電腦上執行的處理常式資源清單,但您可以藉由傳遞 "." 來指定本機電腦。This overload of the GetProcesses method is generally used to retrieve the list of process resources running on a remote computer on the network, but you can specify the local computer by passing ".".

注意

多個 Windows 服務可以在服務主機進程的相同實例中載入 (svchost.exe) 。Multiple Windows services can be loaded within the same instance of the Service Host process (svchost.exe). System.diagnostics.process.getprocesses 不會識別這些個別服務;若要這樣做,請參閱 GetServicesGetProcesses does not identify those individual services; for that, see GetServices.

另請參閱

適用於