using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using System.Windows.Forms;
class Program
{
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;
public static void KeyDown(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
public static void KeyUp(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
private static void OnWindowOpened(object sender, AutomationEventArgs automationEventArgs)
{
try
{
var element = sender as AutomationElement;
if (element != null && Process.GetProcessById(element.Current.ProcessId).MainModule.FileName ==
Environment.GetFolderPath(Environment.SpecialFolder.System)+"\\ApplicationFrameHost.exe")
{
System.Threading.Thread.Sleep(1500);
KeyDown(Keys.F3);
KeyUp(Keys.F3);
System.Threading.Thread.Sleep(200);
KeyDown(Keys.LShiftKey);
KeyDown(Keys.Tab);
KeyUp(Keys.Tab);
KeyUp(Keys.LShiftKey);
KeyDown(Keys.Space);
KeyUp(Keys.Space);
KeyDown(Keys.Escape);
KeyUp(Keys.Escape);
KeyDown(Keys.LControlKey);
KeyDown(Keys.LShiftKey);
KeyDown(Keys.O);
KeyUp(Keys.LControlKey);
KeyUp(Keys.LShiftKey);
System.Threading.Thread.Sleep(200);
KeyDown(Keys.LControlKey);
KeyDown(Keys.LShiftKey);
KeyDown(Keys.I);
KeyUp(Keys.LControlKey);
KeyUp(Keys.LShiftKey);
}
}
catch (ElementNotAvailableException)
{
}
}
static void Main(string[] args)
{
System.Windows.Automation.Automation.AddAutomationEventHandler(
eventId: WindowPattern.WindowOpenedEvent,
element: AutomationElement.RootElement,
scope: TreeScope.Children,
eventHandler: OnWindowOpened);
Process.Start("outlookmail:");
System.Threading.Thread.Sleep(2500);
Automation.RemoveAllEventHandlers();
}
}