Q:

Console application issue with stdin redirection in Windows

I am working on a project to wrap SFTP applications into utility libraries for use in internal company projects. Our company uses Tectia but we're also looking into other libraries like WinSCP. I know there are other open-source SFTP clients out there, but my company insists on supporting Tectia. I know that Tectia supports batch files with the -B switch, but we want the consumers of the library to be able to use the SFTP without needing to learn the syntax and just using .NET methods, so I decided on the path to wrap an interactive console running sftpg3. I am able to start the process, even log in successfully if I include the credentials as command-line parameters. The problem is that I am not able to interact with the process using the redirected StandardInput. Code below:
Process sftpproc = new Process();
sftpproc.StartInfo.FileName = @"sftpg3";
sftpproc.StartInfo.UseShellExecute = false;
sftpproc.StartInfo.RedirectStandardInput = true;
sftpproc.StartInfo.RedirectStandardOutput = true;
sftpproc.StartInfo.RedirectStandardError = true;
sftpproc.StartInfo.CreateNoWindow = true;

StringBuilder sb = new StringBuilder();

sftpproc.OutputDataReceived += 
    (sender, args) => { sb.AppendFormat("INFO:{0}\n", args.Data); };
sftpproc.ErrorDataReceived += 
    (sender, args) => { sb.AppendFormat("WARN:{0}\n", args.Data); };
sftpproc.Start();
sftpproc.BeginOutputReadLine();
sftpproc.BeginErrorReadLine();

StreamWriter input = sftpproc.StandardInput;

input.WriteLine("open USER@SERVER");
input.WriteLine("lcd D:\\");
input.WriteLine("get file.txt");

string messages = sb.ToString();

The code above works perfectly with WinSCP command-line, or when I use the console commands directly in the console. I tried redirecting the output of sftpg3 to a text file, and it is able to redirect it, and then I tried redirecting the input console redirection (sftpg3 <cmd.txt) and it fails to execute the commands (however, -B works, but as mentioned, that is not what I need). Is there a way to force a console application to read from the redirected stdin? Calling the application from cmd.exe didn't work for me.

A:

If sftpg3 < cmd.txt does not work, the application does not read from the standard input.
No matter, where you redirect it from. You simply cannot take this approach.

