2

我想在 c# 中以编程方式运行一个进程。使用 process.start 我可以做到。但是,当该过程要求用户在两者之间输入一些用户输入并在提供输入后再次继续时,我该如何提示用户。

4

2 回答 2

1

只需写入Process.StandardInput

于 2010-06-09T05:11:13.900 回答
0

您可以将事件处理程序添加到 OutputDataReceived 事件。每当进程将一些数据写入其重定向的输出流时,就会调用它。

private StreamWriter m_Writer;

public void RunProcess(string filename, string arguments)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = filename;
    psi.Arguments = arguments;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;

    Process process = Process.Start(psi);
    m_Writer = process.StandardInput;
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
    process.BeginOutputReadLine();
}

protected void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
    // Data Received From Application Here
    // The data is in e.Data
    // You can prompt the user and write any response to m_Writer to send
    // The text back to the appication
}

此外还有一个 Process.Exited 事件来检测您的进程是否退出。

于 2010-06-09T08:00:28.850 回答