Why I cannot get the output of ftp.exe by code?

Posted by smwikipedia on Stack Overflow See other posts from Stack Overflow or by smwikipedia
Published on 2010-03-29T10:24:44Z Indexed on 2010/03/29 10:53 UTC
Read the original article Hit count: 122

Filed under:
|

I execute the ftp.exe cmd through a C# System.Diagnostics.Process type. And I use the following code to get the "ftp.exe" output after I programmatically enter a "help" command. But I can only get the first line of the result. And I never get to the "end" output part. The whole program seems blocked.

    Process p = new Process();
    p.StartInfo.FileName = @"C:\Windows\System32\ftp.exe";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;

    p.StartInfo.UseShellExecute = false;
    p.Start();

    p.StandardInput.WriteLine("help");

    Int32 c_int = p.StandardOutput.Read();
    while (c_int != -1)
    {
        Char c = (Char)c_int;
        Console.Write(c);
        c_int = p.StandardOutput.Read();
    }

    Console.WriteLine("end");

However, I write a simple program which only use Console.Writeline() to write some output to its StdOut stream. And I test it with the above code. It works fine. I just cannot figure out why the above code cannot work with ftp.exe? The only difference between my SimpleConsoleOutput program and the "ftp.exe" is that the ftp.exe has its own interactive command prompt.

© Stack Overflow or respective owner

Related posts about c#

Related posts about command-line