Can you try below. I have now tested it and it is working fine.
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowToFront(string windowName)
{
try
{
IntPtr firstInstance = FindWindow(null, windowName);
ShowWindow(firstInstance, 1);
SetForegroundWindow(firstInstance);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
You have to call the function with the full name of the application that you want to restore. (exactly as seen on the windows task manager -> application tab). You can get the name from process.MainWindowTitle. Check the below event..
private void button1_Click(object sender, EventArgs e)
{
try
{
Process[] Processes = Process.GetProcessesByName("winword");
foreach (Process p in Processes)
{
ShowToFront(p.MainWindowTitle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}