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.
(--------------- New Progress -----------------)
Here're some progress of my personal investigation.
I write 2 threads to write to the StdIn and read from StdOut of "ftp.exe", and the output is like this:
Commands may be abbreviated. Commands are:
Commands may be abbreviated. Commands are:
Commands may be abbreviated. Commands are:
....(exactly 16 times of above lines and then exactly 16 times of the following cmds list)
! delete literal prompt send
? debug ls put status
append dir mdelete pwd trace
...
and the last commands list is not even complete.
It seems that the help command output is divided into two parts.
The 1st part is:
Commands may be abbreviated. Commands are:
The 2nd part is:
! delete literal prompt send
? debug 开发者_运维知识库 ls put status
append dir mdelete pwd trace
...
And all the 1st parts are wrtten to the StdOut stream of "ftp.exe" before all the 2nd parts. How coud this be?? Thanks for your comments.
I tested with other command of the "ftp.exe", and it seems normal except the "help" command
The reason why you cannot get the input and output of ftp.exe is because the built-in ftp.exe from Microsoft Windows 2000/XP/Vista uses Console Input/Output.
It is not simply a case of the ftp program not flushing its buffers.
If you replace your invocation of ftp.exe with something like cmd.exe, you'll see that it works fine. The problem is you are trying to read output where FTP is not sending it.
You cannot use the regular approach to reading and writing to a child ftp.exe. This is a consequence of the implementation of that particular ftp.exe app.
If you truly need to automate the built-in Windows ftp program, you will need to resort to pinvoke and the ReadConsoleOutput win32 function.
Your alternatives are:
- use a different ftp program. Likely they do not resort to the console I/O approach that the MS built-in program does
- use an FTP class, like FtpWebRequest.
- if that's not appropriate or possible, use a low level network socket interface to FTP.
see also: http://discuss.joelonsoftware.com/default.asp?design.4.332503.5
I think the problem is that the output buffer is not flushed yet and hence you don't get the full output from the ftp command.
When you emit a "quit" command you should see the output of the help command.
I have no solution yet, but I'll come back later if I find anything helpful.
ftp.exe just keeps running. You won't get to the 'end' part, since ftp.exe doesn't end when you issue the help command, it presents a prompt and waits for another command.
If you want to read the response of the command, you need to parse the response and look for a new prompt. That is, you got the whole response when you see a line like ftp>
again.
(Unless you have very,very good reasons to use ftp.exe, use the FtpWebRequest class rather)
From experience (I have used command line FTP numerous times before) you would be far better off using an FTP plugin such as this one Enterprise DT FTP.
This way you will have full control over your FTP session, be able to give better feedback to your user and cope with errors in a proper way.
The last point, error handling is very important when dealing with FTP.
Have you tried ReadLine
instead of Read
to read from the redirected output?
Little concerned that you are not closing down the process properly, your code for reading output from a process should look something like:
process.Start();
output = process.StandardOutput.ReadToEnd(); // read the output here...
process.WaitForExit(); // ...then wait for exit, as after exit, it can't read the output
returnCode = process.ExitCode;
process.Close(); // once we have read the exit code, can close the process
Not sure if that would resolve this specific issue though.
Also, why are you writing 'help' to the standard input, does it not work if you do
process.Arguments = "help";
精彩评论