如何将进程CMD.exe行复制到文本框C#

时间:2016-05-06 10:15:53

标签: c# process windows-process

我需要将输出CMD行复制到文本框是否可能? 如果是,请告诉我一些知道如何处理它

enter code here
      private void pictureBox1_Click(object sender, EventArgs e)
       {
        label10.Visible = true;
        string cmd = "/c  adb install BusyBox.apk ";
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = "cmd.exe";
        proc.StartInfo.Arguments = cmd;
        proc.StartInfo.RedirectStandardError = true;

        proc.StartInfo.UseShellExecute = false;
        //proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();

        proc.WaitForExit();
        pictureBox6.Visible = true;
        label10.Text = "Installation Complete";
        // MessageBox.Show("Install Complete ...");
        DateTime Tthen = DateTime.Now;
        do
        {
            Application.DoEvents();
        } while (Tthen.AddSeconds(4) > DateTime.Now);
        label10.Visible = false;
        pictureBox6.Visible = false;

    }

2 个答案:

答案 0 :(得分:3)

你已经根据需要设置了所有内容,唯一缺少的是:

string consoleOutput = proc.StandardOutput.ReadToEnd();

答案 1 :(得分:1)

使用此选项,然后行将包含整个输出

proc.Start();
string line = proc.StandardOutput.ReadToEnd();

或一行

proc.Start();
string line = proc.StandardOutput.ReadLine();

如果你想逐行输出那么

while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    // do your stuff
}

或者你也可以尝试这个,首先删除proc.WaitForExit();因为ReadLine将等待数据可用或流关闭。关闭流后,ReadLine将返回null

string line;
while ((line = proc.StandardOutput.ReadLine())!=null) 
{
    // textbox.text = line or something like that
}