为什么我不能通过代码获取ftp.exe的输出? [英] Why I cannot get the output of ftp.exe by code?

查看:123
本文介绍了为什么我不能通过代码获取ftp.exe的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过C#System.Diagnostics.Process类型执行ftp.exe cmd。我使用下面的代码来获得ftp.exe输出后,我以编程方式输入帮助命令。但我只能得到第一行的结果。我从来没有到达结束输出部分。整个程序似乎被阻止。

  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);然而,我写了一个简单的程序,只使用Console.Writeline()来写一些输出到它的StdOut流。我用上面的代码测试它。它工作正常。我只是不能弄清楚为什么上面的代码不能与ftp.exe一起使用?我的SimpleConsoleOutput程序和ftp.exe之间的唯一区别是ftp.exe有自己的交互式命令提示符。



(------- --------新进度-----------------)



这里是我的一些进步个人调查。



我写了两个线程写入StdIn并从ftp.exe的StdOut读取,输出如下:

 命令可以缩写。命令是:

命令可以缩写。命令是:

命令可以缩写。命令是:
....(正好是以上行的16倍,然后正好是以下cmds列表的16倍)
!删除文字提示send
? debug ls put status
append dir mdelete pwd trace
...

最后的命令列表甚至不完整。



看来help命令输出分为两部分。



第一部分是:

 命令可以缩写。命令是:

第二部分是:

 !删除文本提示send 
? debug ls put status
append dir mdelete pwd trace
...

所有第一部分在所有第二部分之前写入ftp.exe的StdOut流。
这是多么可爱?感谢您的意见。



我使用ftp.exe的其他命令测试,似乎正常,除了help命令

解决方案

原因为什么无法获取ftp.exe的输入和输出是因为内置ftp.exe从Microsoft Windows 2000 / XP / Vista使用控制台输入/输出



这不是简单的ftp程序不刷新其缓冲区的情况。



如果你用cmd.exe替换ftp.exe的调用,你会看到它工作正常。问题是你试图读取FTP不发送的输出。

不能使用常规方法来读取和写入子ftp.exe。这是特定ftp.exe应用程序的实现的结果。






如果您真正需要自动化内置的Windows ftp程序,则需要使用pinvoke和< win32函数的一个href =http://msdn.microsoft.com/en-us/library/ms684965(VS.85).aspx =nofollow noreferrer> ReadConsoleOutput 。



您的选择是:




  • 使用不同的ftp程序。可能他们不诉诸于MS内置程序所使用的控制台I / O方法

  • 使用FTP类,如FtpWebRequest。

  • 如果这不适合或可能,使用低级网络套接字接口FTP。






另请参阅: http://discuss.joelonsoftware.com/default.asp?design.4.332503.5


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

这篇关于为什么我不能通过代码获取ftp.exe的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆