C# program to automate Reflection FTP Client connecting via TLS 1.2 to an IBM Host

  • 7025204
  • 21-Jul-2021
  • 06-Aug-2021

Environment

Reflection FTP Client

Situation

How to automate the Reflection FTP Client using C# to build a secure TLS 1.2 connection to an IBM Host system.

Resolution

A simple C# Console program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReflectionFTP;

namespace FTPCSharpConsoleSFTP
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("FTP Test Program Starting...");

            // information for connection to a FTP Server named "mainframe.mycompany.com"
            // make sure to have the certificate for the FTP Server is installed locally in Microsoft IE Certificate store in the Trusted Root Store
            string serverAddress = "mainframe.mycompany.com";
            string serverUser = "myname";
            string serverPassword = "mypassword";
            int serverPort = 21000;

            // for IBM Connections
            ReflectionFTP15 rftp = new ReflectionFTP15();

            rftp.UseTLS = (int)ReflectionFTP.Constants.rcTrue;              // enable TLS
            rftp.SSLVersion = ReflectionSSLVersion.SSLVersionTLSv1_2;       // set for TLS 1.2
            rftp.TLSEncryptData = (int)ReflectionFTP.Constants.rcTrue;      // encrypt both the command and data channel
            rftp.PKIDisableCRL = (int)ReflectionFTP.Constants.rcTrue;       // disable CRL checking
            rftp.PKIUseOCSP = (int)ReflectionFTP.Constants.rcFalse;         // disable OCSP checking
            rftp.QuietStatus = (int)ReflectionFTP.Constants.rcTrue;         // disable banners

            Console.WriteLine("FTP Test Program Connecting...");
           
            // Connect to the FTP Server
            rftp.Open(serverAddress, serverUser, serverPassword, null, serverPort, (int)ReflectionFTP.Constants.rcTrue);
            if (rftp.LastError != 0)
            {
                    Console.WriteLine("Open Failed - Terminating Program - rftp.LastErrorString = " + rftp.LastErrorString);
                    Console.ReadKey();
                    System.Environment.Exit(1);
            }
            Console.WriteLine("    FTP Test Program Connected...");

            // read and display a few files on the FTP server to confirm connection
            string filter = "";  
            string folder = "/test/";
            rftp.SetCurrentDirectory(folder);
            rftp.OpenDirectoryListing(filter);
            for (int i = 0; i < 3; i++)
            {
                rftp.GetNextFileDetails();
                Console.WriteLine("        FTP File Name: " + rftp.FileName);
            }

            // close out the FTP connection
            Console.WriteLine("    FTP Test Program Closing...");
            rftp.Close();
            if (rftp.LastError != 0)
            {
                    Console.WriteLine("Closed Failed - Terminating Program");
                    Console.WriteLine("Closed Failed - rftp.LastErrorString = " + rftp.LastErrorString);
                    Console.ReadKey();
                    System.Environment.Exit(1);
            }
            
            // say goodbye
            Console.WriteLine(" ");
            Console.WriteLine("FTP Test Program Complete - Press any key...");
            Console.ReadKey();
        }
    }
}


Note:
  • Include the "Reflection FTP 1.15 Type Library" under References in the C# Solution so the code will properly compile without errors.
  • Make sure to locally install the FTP Server certificate in the Microsoft Windows Internet Explorer Certificate store (in the Trusted Root Store).