In my Windows application, I want to be able to switch to another application running on my computer. For example, I want to switch to Internet Explorer and make it the active window. How do I do that?
To solve this problem, you need to first obtain the list of processes currently running on your system. You then need to find the required application/process to switch to and then make that window active.
First, import the following namespace:
Then, declare the following functions from the user32.dll (yes, you will use P/Invoke to switch the current active window):
public partial class Form1 : Form
private static extern bool
private static extern bool ShowWindowAsync(
IntPtr hWnd, int nCmdShow);
Define the following constant:
private const int SW_RESTORE = 9;
Assuming that you want to switch to IE, the following function will retrieve the list of processes running, look for IE, and then make it the active window:
private void SwitchToIE()
Process procs = Process.GetProcesses();
if (procs.Length != 0)
for (int i = 0; i < procs.Length; i++)
if (procs[i].MainModule.ModuleName ==
IntPtr hwnd =
MessageBox.Show("No process running");
"Internet Explorer is not running");
Voila! If at least one instance of IE is running, you will be able to switch to it; else it will display the message “Internet Explorer is not running”.