다음을 통해 공유


How to: Bind 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 How to: Create Process Components.

  2. Declare an empty array of the type Process to hold the array of Process components.

    Dim myProcesses() As Process
    
    Process[] myProcesses;
    
  3. Fill the process array by calling the GetProcessesByName or GetProcessById method 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.

    myProcesses = Process.GetProcessesByName("Notepad")
    
    myProcesses = Process.GetProcessesByName("Notepad");
    
  4. Use the indexed property value to manipulate a single process in the array.

    ' Closes the first instance of Notepad in the process array.
    myProcesses(0).CloseMainWindow()
    
    // Closes the first instance of Notepad in the process array.
    myProcesses[0].CloseMainWindow();
    

See Also

Tasks

How to: Specify Processes
How to: Stop Processes

Concepts

Introduction to Monitoring and Managing Windows Processes

Other Resources

Retrieving Information About Processes