/// /// This program demo's the basic of launching and ICO session from within an application. /// class Program { /// /// Auto Reset Event used to block execution until we receive the OnLogon event /// public static AutoResetEvent onLogonResetEvent = null; static void Main(string[] args) { ICAClientClass ica = new ICAClientClass(); onLogonResetEvent = new AutoResetEvent(false); // Launch published Notepad if you comment this line, and uncommented // the one above you will launch a desktop session // ica.Application = ""; ica.InitialProgram = "#Notepad"; // Launch a new session ica.Launch = true; // Set Server address ica.Address = "10.8.X.X"; // No Password property is exposed (for security) // but can explicitly specify it by using the ICA File "password" property ica.Username = "johncat"; ica.Domain = "xxxx"; ica.SetProp("Password", "xxxx"); // Let's have a "pretty" session ica.DesiredColor = ICAColorDepth.Color24Bit; // Reseach the output mode you want, depending on what your trying // to attempt to automate. The "Client Simulation APIs" are only available under certain modes // (i.e. things like sending keys to the session, enumerating windows, etc.) ica.OutputMode = OutputMode.OutputModeNormal; // Height and Width ica.DesiredHRes = 1024; ica.DesiredVRes = 786; // Register for logon event ica.OnLogon += new _IICAClientEvents_OnLogonEventHandler(ica_OnLogon); // Launch/Connect to the session ica.Connect(); if (onLogonResetEvent.WaitOne(new TimeSpan(0, 2, 0))) Console.WriteLine("Session Logged on sucessfully! And OnLogon Event was Fired!"); else Console.WriteLine("OnLogon event was NOT Fired! Logging off ..."); // Do we have access to the client simulation APIs? if (ica.Session == null) Console.WriteLine("ICA.Session object is NULL! :("); // Give notepad a few seconds to setup // (or detect the window as seen below) // Obvious Tips: Sleep is usually bad in automation, race conditions ... now you know Thread.Sleep(5000); //////////////////////////////////////////////////////////////////// // Keyboard Simulation // Let's script some key input with the Keyboard interface Keyboard kbrd = ica.Session.Keyboard; // "Greatness has been achieved!" - Indeed! kbrd.SendKeyDown((int)Keys.G); kbrd.SendKeyDown((int)Keys.R); kbrd.SendKeyDown((int)Keys.E); kbrd.SendKeyDown((int)Keys.A); kbrd.SendKeyDown((int)Keys.T); kbrd.SendKeyDown((int)Keys.N); kbrd.SendKeyDown((int)Keys.E); kbrd.SendKeyDown((int)Keys.S); kbrd.SendKeyDown((int)Keys.S); kbrd.SendKeyDown((int)Keys.Space); kbrd.SendKeyDown((int)Keys.H); kbrd.SendKeyDown((int)Keys.A); kbrd.SendKeyDown((int)Keys.S); kbrd.SendKeyDown((int)Keys.Space); kbrd.SendKeyDown((int)Keys.B); kbrd.SendKeyDown((int)Keys.E); kbrd.SendKeyDown((int)Keys.E); kbrd.SendKeyDown((int)Keys.N); kbrd.SendKeyDown((int)Keys.Space); kbrd.SendKeyDown((int)Keys.A); kbrd.SendKeyDown((int)Keys.C); kbrd.SendKeyDown((int)Keys.H); kbrd.SendKeyDown((int)Keys.I); kbrd.SendKeyDown((int)Keys.E); kbrd.SendKeyDown((int)Keys.V); kbrd.SendKeyDown((int)Keys.E); kbrd.SendKeyDown((int)Keys.D); // Exclamation ! kbrd.SendKeyDown((int)Keys.ShiftKey); kbrd.SendKeyDown((int)Keys.D1); //////////////////////////////////////////////////////////////////// // ScreenShots // Let's take a screen shot of our session ScreenShot ss = ica.Session.CreateFullScreenShot(); ss.Filename = Path.GetTempPath() + Path.GetRandomFileName() + ".bmp"; Console.WriteLine("Saving Screen Shot to: " + ss.Filename); ss.Save(); /////////////////////////////////////////////////////////////////// // Session Windows Enumeration // Enumerate top level windows and output them Windows allTopLevelWindows = ica.Session.TopLevelWindows; if (allTopLevelWindows.Count > 0) { Console.WriteLine("\nDisplaying all Top Level Windows:"); foreach (window w in allTopLevelWindows) { Console.Write("\n"); Console.WriteLine("Window ID: " + w.WindowID); Console.WriteLine("Window Caption: " + w.Caption); Console.WriteLine("Window Position X: " + w.PositionX); Console.WriteLine("Window Position Y: " + w.PositionY); } } Console.WriteLine("\nPress any key to log off"); Console.Read(); // Logoff our session Console.WriteLine("Logging off Session"); ica.Logoff(); } /// /// OnLogon Event Handler /// static void ica_OnLogon() { Console.WriteLine("OnLogon event was Handled!"); onLogonResetEvent.Set(); } }