This documentation is archived and is not being maintained.

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

  1. Create an instance of the Process component. For more information, see Creating Process Components.
  2. Declare an empty array of the type Process to hold the array of Process components.
    ' Visual Basic
    Dim myProcesses() as Process
    
    // C#
    Process[] myProcesses;
    
  3. 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");
    
  4. 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[0].CloseMainWindow();
    

See Also

Starting Processes | Stopping Processes | Retrieving Information About Processes | Introduction to Monitoring and Managing Windows Processes

Show: