Binding to Existing Processes
You can use the Process component as a connection between your application and the processes on your computer. You bind to an existing process by using the GetProcessesByName method to return an array of processes (in the form of Process component instances) associated with each running instance of the specified file name. The first element of the array will be the first instance of the running process that the method finds. If there is only one instance of the process, the index of 0 can be used to reference the single instance of the process. You can use each of these returned instances to manipulate the processes with which they are associated.
To bind to an existing process
- Create an instance of the Process component. For more information, see Creating Process Components.
- Declare an empty array of the type Process to hold the array of Process components.
' Visual Basic Dim myProcesses() as Process // C# Process myProcesses;
- Fill the process array by calling the GetProcessesByName or GetProcessById methods and use the returning value to populate the process array.
The following example shows how to call the GetProcessesByName method to add Notepad.exe to the process array.
' Visual Basic myProcesses = Process.GetProcessesByName("Notepad") // C# myProcesses = Process.GetProcessesByName("Notepad");
- Use the indexed property value to manipulate a single process in the array.
' Visual Basic ' Closes the first instance of Notepad in the process array. myProcesses(0).CloseMainWindow() // C# // Closes the first instance of Notepad in the process array. myProcesses.CloseMainWindow();