2

我有一个用 Delphi 编写的控制台应用程序 (Host.exe)。它接受标准输入 readln 和 writeln 对标准输出的响应。现在,我想以 C# 向 Host.exe 提供输入并从 Host.exe 获取输出的方式在 C# 应用程序中使用 Host.exe 理想情况下,我编写了下面的代码,但它不起作用:它挂在outputReader.ReadLine();

System.IO.File.WriteAllText(tmp, vbs);

Process pProcess = new Process();

pProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = @"Host.exe";
pProcess.StartInfo.Arguments = "\"runa " + tmp +"\"";
// runs script file tmp in background
pProcess.StartInfo.CreateNoWindow = true;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardInput = true;
pProcess.StartInfo.RedirectStandardError = true;

pProcess.Start();

StreamWriter inputWriter = pProcess.StandardInput;
StreamReader outputReader = pProcess.StandardOutput;

while (true)
{
    inputWriter.WriteLine("getmsg");
    inputWriter.Flush();
    string s = outputReader.ReadLine(); // then do something with it

    inputWriter.WriteLine("progressglobal");
    inputWriter.Flush();

    string p = outputReader.ReadLine();
    if (p == "100")
    {
        break;
    }
    Application.DoEvents();
    Thread.Sleep(1000);
}
inputWriter.WriteLine("exit");
inputWriter.Flush();                    
pProcess.WaitForExit();

非常感谢您提前提出任何建议!

4

1 回答 1

0

你读了两遍:

string s = outputReader.ReadLine();

string p = outputReader.ReadLine();

似乎您只需要最后一行,因为s未使用该变量。

于 2012-10-12T05:51:01.627 回答