进程输出不写最后一行

时间:2016-06-25 13:40:16

标签: c# process

我正在为控制台程序制作GUI。有时需要输入密码,因此如果输出包含“密码”,则会打开一个对话框。

问题是输出不会打印“输入密码”这一行,而是会运行控制台。

  

打开...
  阅读配置......
  输入您的密码:< - 它出现在控制台中,但不出现在GUI中。

过程:

this.ASFProcess.StartInfo.Arguments = "--server";
this.ASFProcess.StartInfo.CreateNoWindow = true;
this.ASFProcess.StartInfo.Domain = "";
this.ASFProcess.StartInfo.FileName = "ASF.exe";
this.ASFProcess.StartInfo.LoadUserProfile = false;
this.ASFProcess.StartInfo.Password = null;
this.ASFProcess.StartInfo.RedirectStandardError = true;
this.ASFProcess.StartInfo.RedirectStandardInput = true;
this.ASFProcess.StartInfo.RedirectStandardOutput = true;
this.ASFProcess.StartInfo.StandardErrorEncoding = null;
this.ASFProcess.StartInfo.StandardOutputEncoding = null;
this.ASFProcess.StartInfo.UserName = "";
this.ASFProcess.StartInfo.UseShellExecute = false;
this.ASFProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
this.ASFProcess.SynchronizingObject = this;
this.ASFProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(this.ASFProcess_OutputDataReceived);
this.ASFProcess.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(this.ASFProcess_ErrorDataReceived);

方法:

private void BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    ASFProcess.Start();
    ASFProcess.BeginOutputReadLine();
    ASFProcess.BeginErrorReadLine();
    ASFProcess.WaitForExit();
}

private void ASFProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    rtbOutput.AppendText(e.Data + Environment.NewLine);
    rtbOutput.SelectionStart = rtbOutput.Text.Length;
    rtbOutput.ScrollToCaret();
    if (e.Data.Contains("password"))
    {
        var enterPassword = new EnterPassword();
        enterPassword.ShowDialog();
    }
}

1 个答案:

答案 0 :(得分:0)

根据这个:https://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived(v=vs.110).aspx

  

OutputDataReceived事件表示关联的进程具有   写了一个

     

行,以换行符结尾,重定向   StandardOutput流。

所以我建议ASF.exe程序的问题在于它提示输入密码的行,它在预期输入之前没有发送换行符。

我建议改变你的ASF程序的工作方式,或尝试阅读标准版和标准版。错误输出以另一种方式输出。

这可能会对您有所帮助:

https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx