0

I have tried to run the batch file from c# using the following code and i want to display the result in WPF textbox. Could you please guide me how to do this?

using System;

namespace Learn
{
    class cmdShell
    {
        [STAThread]  // Lets main know that multiple threads are involved.
        static void Main(string[] args)
        {
            System.Diagnostics.Process proc; // Declare New Process
            proc = System.Diagnostics.Process.Start("C:\\listfiles.bat"); // run test.bat from command line.
            proc.WaitForExit(); // Waits for the process to end.
        }
    }
}

This batch file is to list the files from the folder. Once the batch is executed result should be displayed in the textbox. If the batch file having more than one commands, then result of each commands should be displayed in textbox.

1
  • The result are furring from a different file thus different process, you can access your application using Pipe (or WCF) you won't be able to fish out those results Commented Oct 24, 2013 at 10:27

2 Answers 2

2

You need to redirect the standard output stream:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "test.bat";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            string output = proc.StandardOutput.ReadToEnd();
            Console.WriteLine(output); // or do something else with the output
            proc.WaitForExit();
            Console.ReadKey();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yes this working fine. but output is displayed once the execution completed, is there anything need to add for this?
The StandardOutput property of the Process class is a StreamReader instance. You don't need to wait for the execution to end. For example: if you use proc.StandardOutput.ReadLine() in a loop you can wait for the next line to be written and push it to your textbox immediately. See the documentation for more information.
0

I have resolved the issues with process hanging and getting output instantly as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = "test.bat";
            proc.StartInfo.UseShellExecute = false;
           proc.StartInfo.RedirectStandardOutput = true;
            proc.OutputDataReceived += proc_OutputDataReceived;
            proc.Start();
            proc.BeginOutputReadLine();
        }
    }


        void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            this.Dispatcher.Invoke((Action)(() =>
                        {
                            txtprogress.Text = txtprogress.Text + "\n" + e.Data;
                            txtprogress.ScrollToEnd();
                        }));
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.