我写了编程以获取有关Android设备的信息 我使用外部流程 创建类apktool: 这段代码
命名空间dream_tool { class apktool {
public StringBuilder output = new StringBuilder();
public Process cmd = new Process();
public string RunExternalPing(string a)
{
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.Arguments = a;
cmd.EnableRaisingEvents = true;
cmd.OutputDataReceived +=
new DataReceivedEventHandler(cmd_OutputDataReceived);
//cmd.Exited += new EventHandler(cmd_Exited);
cmd.Exited += new EventHandler(Form1.instance.cmd_Exited);
cmd.Start();
cmd.BeginOutputReadLine();
cmd.WaitForExit();
// StreamWriter sw = cmd.StandardInput;
//sw.WriteLine(a);
// cmd.WaitForExit();
// sw.Close();
return a;
}
private void cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Form1 frm = new Form1();
if (!String.IsNullOrEmpty(e.Data))
{
output.Append(e.Data + Environment.NewLine);
}
cmd.CancelOutputRead();
}
}
} 并以此代码的形式
public partial class Form1 : Form
{
public static Form1 instance;
apktool a2;
Procees_adb a1 = new Procees_adb();
List<String> fullFileName;
public Form1()
{
InitializeComponent();
instance = this;
a2 = new apktool();
}
public void cmd_Exited(object sender,EventArgs e) {
if (RCH_OUT.InvokeRequired)
{
RCH_OUT.Invoke(new EventHandler(delegate { RCH_OUT.Text = (a2.output.ToString()); }));
}
else
{
RCH_OUT.Text = (a2.output.ToString());
MessageBox.Show(a2.output.ToString());
}
if(IsDisposed)
{
a2.cmd.Dispose();
}
//cmd.Dispose();
}
private void button2_Click(object sender,EventArgs e) {
a2.RunExternalPing("/c adb shell getprop ro.product.cpu.abi");
}
}
[当我第一次按下按钮时,我得到信息,那很好 1
如果我再次按下它 它显示我在cmd.BeginOutputReadLine();
上的错误System.InvalidOperationException:&#39;已经在流上启动了异步读取操作。&#39; enter image description here
通过添加cmd.CancelOutputRead();
解决第一个问题到
private void cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
但是现在当第二次按下按钮信息重复时 比如图中的那个 enter image description here