It's not very nice! But it works:
[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains("Notepad"))
{
SetForegroundWindow(proc.MainWindowHandle);
Thread.Sleep(1000);
SendKeys.SendWait("^C");
if (Clipboard.ContainsText())
{
string text = Clipboard.GetText(TextDataFormat.UnicodeText);
Console.WriteLine(text);
}
else
{
Console.WriteLine("No Text");
}
}
}
}
You can cut down the sleep value, but you do need to suspend the task briefly to allow the set foreground window to work.