0

I have a System.Diagnostics.Process variable named _program.

I understand that not every process have a user interface, so it will never have a focus (I think).

But, supposing that this process has a interface, is it possible to set focus to it ? Maybe I need to use Process.StandardInput?

  • 2
    Possible duplicate of [Correct way (in .NET) to switch the focus to another application](https://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application) –  Oct 10 '18 at 18:27

1 Answers1

3

Use PInvoke to call a native function to set the foreground window:

using System.Diagnostics;
using System.Runtime.InteropServices;

[DllImport("User32.dll")]
private static extern Int32 SetForegroundWindow(nint hWnd);

void YourMethod()
{
  Process p = ... // However you create your process
  SetForegroundWindow(p.MainWindowHandle);  // Set this process's main window as the foreground window
}
MikeH
  • 4,242
  • 1
  • 17
  • 32