Search code examples
c#.netftpmainframe

How do I run a STOR cmd to an FTP server on a mainframe using the FTPClient object in C#


I am trying to run a JCL code file on a mainframe to JES in C# code. I first execute the SITE command telling it that the file type is JES and then try to run a STOR command on the .txt file with the JCL code. The SITE command runs well and returns success code 200, but the STOR command fails and throws a timeout exception.

using (FtpClient ftp = new FtpClient())
{
    ftp.Host = host;
    ftp.InternetProtocolVersions = FtpIpVersion.IPv4;
    ftp.Credentials = new NetworkCredential("login","pass");
    ftp.DataConnectionType = FtpDataConnectionType.PASV;
    ftp.EncryptionMode = FtpEncryptionMode.None;
    ftp.Port = 21;                    
    ftp.Connect();
    bool isConnected = ftp.IsConnected;
    FtpReply site = ftp.Execute("SITE FILETYPE=JES");
    FtpReply stor = ftp.Execute("STOR " + file);
    ftp.Disconnect();
    ftp.Dispose();
    bool isDisposed = ftp.IsDisposed;
}

Solution

  • I was able to run a STOR or PUT command from my C# .NET app by running a batch file with all the commands from the cmd prompt window using the Process.Start() method in my .NET C# app. I was trying to run some JCL code in JES location of mainframe. If you run this on a server or a localhost dev machine make sure that your FTP capabilities are configured otherwise it will not run. Test ftp commands in cmd prompt window first to make sure your machine is FTP enabled. For me it worked well in dev and then had FTP issues in server. Batch file extension I used was .txt and it worked. I am sure you can test using .bat or .ftp and it will also work.

    1. Create a batch file with content something like this:

      open [mainframe-name-here]

      [username-here]

      [password-here]

      quote SITE FILETYPE=JES

      ascii

      put [filename-here]

      bye

    2. Run the batch file opening cmd line prompt window in Windows using the ProcessStartInfo and Process class:

      ProcessStartInfo startInfo = new ProcessStartInfo();

      startInfo.UseShellExecute = false;

      startInfo.CreateNoWindow = true;

      startInfo.WindowStyle = ProcessWindowStyle.Hidden;

      startInfo.WorkingDirectory = workingDir;

      startInfo.FileName = "cmd.exe";

      startInfo.Arguments = "/c ftp -i -s:" + batchFileName;

      Process process = new Process();

      process.StartInfo = startInfo;

      Thread.Sleep(100); // required min stop time to let operations catch up

      process.Start();

      process.Dispose();

    If anyone has any questions on this let me know and I can help you.