Thursday, February 27, 2020

Run CMD From C sharp

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DockerRunner
{
    delegate void SetMergerTextCallback(string text);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Process _Process;
        private void Button1_Click(object sender, EventArgs e)
        {
            Execute("echo chamith");
        }
        private Process ConfigureFfmpeg()
        {
            _Process = new Process();
            _Process.StartInfo.FileName = "cmd";
            _Process.StartInfo.UseShellExecute = false;
            _Process.StartInfo.RedirectStandardOutput = true;
            _Process.StartInfo.RedirectStandardInput = true;
            _Process.StartInfo.CreateNoWindow = true;
            return _Process;
        }

        private void Execute(string argument)
        {

            var process = ConfigureFfmpeg();
           process.OutputDataReceived += p_OutputDataReceived;
            process.Start();
            process.StandardInput.WriteLine(argument);
            process.BeginOutputReadLine();
        }
        StringBuilder sb = new StringBuilder();
        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            sb.Append(e.Data + Environment.NewLine);
            SetText(sb.ToString());
        }

        private void SetText(string text)
        {
            if (this.richTextBox1.InvokeRequired)
            {
                SetMergerTextCallback d = new SetMergerTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.richTextBox1.Text = text;
            }
        }
    }
}

No comments:

Post a Comment

Sql server row level policy