To Capture Console's output

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

namespace ConsoleWPF
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var p = new Process();
            Task.Factory.StartNew(() =>
            {
                var pInfo = new ProcessStartInfo("cmd.exe", "/c dir")
                {
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true,
                };

                //var ms = new MemoryStream();
                //var streamWriter = new StreamWriter(ms);


                p.StartInfo = pInfo;
                p.EnableRaisingEvents = true;
                p.OutputDataReceived += proc_DataReceived;
                p.ErrorDataReceived += proc_DataReceived;
                p.Start();
                p.BeginErrorReadLine();
                p.BeginOutputReadLine();

                p.WaitForExit();
            });
        }
        void proc_DataReceived(object sender, DataReceivedEventArgs e)
        {
            TextBox1.Dispatcher.BeginInvoke(
                new Action<string>(
                    (s) =>
                    {
                        AppendLine(s);
                    }), e.Data);
        }
        private void AppendLine(string msg)
        {
            TextBox1.Text += Environment.NewLine + msg;
        }
    }
}

댓글

이 블로그의 인기 게시물

Oracle NLS_DATE_FORMAT 변경

Stop console process using Ctrl+C.

Alternative to IValueConvert, QuickConverter