I have been trying to figure this for hours, and can't seem to find a straight answer. I have found similar questions but none seem to work with what I am trying to do here. I am trying to avoid having to write to a file. If I can redirect to a variable, that would be great.
Given the following code:
static void Main(string[] args) {
const int VK_RETURN = 0x0D;
const int WM_KEYDOWN = 0x100;
var cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.Start();
using (var stdin = cmd.StandardInput) {
stdin.WriteLine("ftp");
stdin.WriteLine("open localhost");
stdin.WriteLine("anonymous");
var hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
stdin.WriteLine("ls");
stdin.WriteLine("close localhost");
stdin.WriteLine("bye");
}
cmd.WaitForExit();
cmd.Close();
}
How do I redirect the ls output to a variable?
I was able to grab output from the console by handling the OutputDataReceived event. https://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived(v=vs.110).aspx