繁体   English   中英

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

[英]How To Copy Process CMD.exe line to Textbox C#

我需要将“输出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;

    }

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

string consoleOutput = proc.StandardOutput.ReadToEnd();

使用它,那一行将包含整个输出

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM