Output Command Line (.Bat FILE) show in textbox with c#

649 views Asked by At
        Process proc = new Process();
        proc.StartInfo.FileName = @"C:\Users\Administrator\Desktop\Python27\ToolArtworkEmoji.bat";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
        proc.OutputDataReceived += proc_OutputDataReceived;
        proc.BeginOutputReadLine();
        proc.WaitForExit();
        textbox1.Text = proc.StandardOutput.ReadToEnd();

Why code not active. It not show command in textbox. Can I help me? Thank you very much

2

There are 2 answers

0
Denis  Yarkovoy On BEST ANSWER

You need to read the output in your proc_OutputDataReceived handler, and not via proc.StandardOutput.ReadToEnd();

In other words, put something like

textbox1.BeginInvoke(new Action(()=>{textbox1.Text=e.Data;}));

in the proc_OutputDataReceived handler (e is a DataReceivedEventArgs argument)

0
Huynh Bao On
            private void button1_Click_1(object sender, EventArgs e)
    {
        Process proc = new Process();
        proc.StartInfo.FileName = @"C:\Users\Administrator\Desktop\Python27\ToolArtworkEmoji.bat";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
        proc.OutputDataReceived += proc_OutputDataReceived;
        proc.BeginOutputReadLine();
        proc.WaitForExit();
    {

    private void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        tblog.BeginInvoke(new Action(() => { tblog.Text = e.Data; }));
    }

E had Edited. And it doesn't active. Textbox not show anything.