Hello Everyone,
I am not sure what am I doing wrong here. I spend almost whole day working
on this. I am trying to set focus on the process that I started by
Process.start. Below is my code
Process process;
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = @"C:\Program Files\test.exe";
process.StartInfo.WorkingDirectory = @"C:\Program Files\";
process.Start();
IntPtr hWnd =
System.Diagnostics.Process.GetProcessById(process. Id).MainWindowHandle;
if (!hWnd.Equals(IntPtr.Zero))
{
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
SetForegroundWindow(hWnd);
}
if (process.Responding)
{
SendKeys.SendWait("{ENTER}");
}
else
{
process.Kill();
}
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow (IntPtr hWnd);
while debugging my code never goes inside this if
(!hWnd.Equals(IntPtr.Zero)) statement. I tried to rewrite the code after
reading lot of articles on internet, but it didn't work. Can anyone please
help me on this. I am trying to setfocus on the "test" program so that I can
press enter key, but since there is no focus on the test program, the enter
key is useless. I can start the program successfully, but I cannot put the
focus on that program so that enter key will work.
Any help will be greatly appreciated