我正在使用下面的代码
System::Diagnostics::Process^ p = gcnew System::Diagnostics::Process();
p->StartInfo->FileName = "tnccmd.exe";
p->StartInfo->UseShellExecute = false;
p->StartInfo->RedirectStandardInput = true;
p->StartInfo->RedirectStandardOutput = true;
p->Start();
System::IO::StreamWriter^ tnc_stdin = p->StandardInput;
System::IO::StreamReader^ tnc_stdout = p->StandardOutput;
tnc_stdin->WriteLine("connect i 127.0.0.1");
String^ prg_output = tnc_stdout->ReadToEnd();
我的问题是我无法正确读取 stdout
。不过,我可以轻松地写入 stdin
,但现在我正在尝试实现一些错误检查代码,但它不起作用。
我正在使用的程序似乎没有写入 stdout
,即使它是在命令行中运行的。我可以使用 Windows XP
默认附带的 ftp.exe
重现 bug
。如果您将 ->FileName
更改为 ftp.exe
命令提示符 ftp.exe
通常会给出 ftp>
不会出现在 prg_output
中。
现在我知道提示必须使用某种windows shell curses
,我可能会混淆问题。
通常在 connect i 127.0.0.1
指令之后,我应该收到 connecting to 127.0.0.1...
但我什么也没收到。
关于我做错了什么的任何提示?是否还有另一种我不知道的stdout
?
编辑
我不能使用参数,因为我要写多行,很像 ftp.exe
。
此外,ftp.exe
会在您键入 dir 等命令时输出。至少当你写未知命令时它会输出,它会提示 Invalid command
。
最佳答案
我怀疑您正试图向标准输入发送实际上应该是命令行参数的内容。您通常如何调用 tnccmd.exe?是这样的吗?
tnccmd.exe connect i 127.0.0.1
如果是这样,那么“connect i 127.0.0.1”不应该在标准输入上继续,而应该通过 p->StartInfo->Arguments 传递。
(ftp.exe 的问题不在于你的程序,而在于 ftp.exe 本身,它会检查它的标准输出是否是控制台。如果它的输出不在控制台上,那么它不会输出“ftp>” 提示符。您尝试编写脚本的程序也有可能做同样的事情。)
关于.net - 使用 .net 从子进程重定向标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/318915/