Penerapan Process.GetProcesses Method

Share:

Membuat larik komponen Proses baru dan mengaitkannya dengan sumber daya proses yang ada.

Contoh berikut mengambil informasi dari proses saat ini, proses yang berjalan di komputer lokal, semua contoh Notepad yang berjalan di komputer lokal, dan proses tertentu di komputer lokal. Kemudian mengambil informasi untuk proses yang sama di komputer jarak jauh.


using  System.Diagnostics.Process


Process currentProcess = Process.GetCurrentProcess();
Console.WriteLine(currentProcess.ProcessName);
Console.WriteLine(currentProcess.Id); 



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();
            	Console.WriteLine(currentProcess.ProcessName);
				Console.WriteLine(currentProcess.Id); 
            

            // 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();
        }
    }
}



myComputer = Environment.MachineName;
//Get all processes on a remote computer.
Process[] remoteAll = Process.GetProcesses("myComputer");

//Output
Array.ForEach(remoteAll, (process) =>
{
	Console.WriteLine("Process: {0} Id: {1}",
	process.ProcessName, process.Id);
});


ads Bottom