【发布时间】:2016-06-25 13:40:16
【问题描述】:
我正在为控制台程序制作 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();
}
}
【问题讨论】: