Reflection 2014 .NET API Guide
Create a Session From an Existing Session File

This sample program uses CreateControl() to create a new session based on a saved session file. The session does not have a view associated with it and is therefore not visible. (For other ways to get terminal sessions, see Getting Terminal and View Objects.)

 

To create an "existing" session

  1. Start Reflection, create a session document, and connect to the host.
  2. Name, save, and then close the session document, but leave Reflection running.
  3. In Visual Studio, create a new Console Application project. In the New Project dialog box, type "Get Existing Session" in the Name field. Select the Create directory for solution box, click Browse to select your API projects folder (for example, C:\MyProjects), and then click OK.
  4. In Solution Explorer, add the class assemblies. (For instructions, see Create an API Project.)
  5. Copy the code from one of the following programs:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Attachmate.Reflection.Framework;
    using Attachmate.Reflection.Emulation.IbmHosts;
     
    namespace CreateSessionFromExistingSessionFile
    {
      class Program
      {
        static void Main(string[] args)
        {
          Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication("myWorkspace", true);
          if (reflectionApplication != null)
          {
            IIbmTerminal terminal = (IIbmTerminal)reflectionApplication.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\[session filename]");
            if (terminal != null)
            {
              terminal.Connect();
              //You can also use AfterConnect event to wait for the connection.
              while (!terminal.IsConnected)
              {
                System.Threading.Thread.Sleep(500);
              }
              IIbmScreen screen = terminal.Screen;
              screen.WaitForHostSettle(6000, 3000);
              string text = screen.GetText(18, 2, 48);
              Console.WriteLine(text);
            }
            else
              Console.WriteLine("Can not create the control. Please check the session file path.");
          }
          else
            Console.WriteLine("Failed to get Application object.");
        }
      }
    }
    
                   
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Attachmate.Reflection.Framework;
    using Attachmate.Reflection.Emulation.OpenSystems;
     
    namespace CreateSessionFromExistingSessionFile
    {
      class Program
      {
          Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication("myWorkspace", true);
          if (reflectionApplication != null)
          {
            ITerminal terminal = (ITerminal)reflectionApplication.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\[session filename]");
            if (terminal != null)
            {
              terminal.Connect();
              //You can also use AfterConnect event to wait for the connection.
              while (!terminal.IsConnected)
              {
                System.Threading.Thread.Sleep(500);
              }
              IScreen screen = terminal.Screen;
              string text = screen.GetText(18, 2, 48);
              Console.WriteLine(text);
            }
            else
              Console.WriteLine("Can not create the control. Please check the session file path.");
          }
          else
            Console.WriteLine("Failed to get Application object.");
      }
    }
    
                     
  6. In Visual Studio, paste the code to the Program.cs tab, replacing all existing code.
  7. In the code, change the parameter for reflectionApplication.GetControlsByFilePath to the complete path of your saved session document. Between the quotation marks, type the path exactly as it appears, including the drive letter (for example, C:\), any spaces, and the filename and extension of the session document.
  8. Build the application (Build > Rebuild Solution) and then run it (Debug > Start Without Debugging).

    A Windows command console containing a line of extracted text should appear.