111,094
社区成员




class a {
public string NewAccount { get;set;}
public void run()
{
process.StartInfo.FileName = @"D:\ran.exe";
process.StartInfo.Arguments = " create account ";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.BeginOutputReadLine();
// 为异步获取订阅事件
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
}
private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (String.IsNullOrEmpty(e.Data) == false)
this.AppendText(e.Data + "\r\n");
}
public delegate void AppendTextCallback(string text);
public void AppendText(string text)
{
console += text;
if (console.IndexOf("input pwd") > -1 && !setPwd)
{
SetPwd(process);
setPwd = true;
}
if(console.IndexOf("new account is {")> -1)
{
string account_id= console.Substring(console.IndexOf("{") + 1);
account_id= account_id.Substring(0, account_id.IndexOf("}"));
//这里已经可以得到account id
NewAccount =account_id
}
}
}
//调用代码
A a = new A();
a.run();
string account = a.NewAccount;
//这里只能得到null