Advertisement
Jimi2000

ProcessCommandConsole

Aug 3rd, 2018 (edited)
1,308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.02 KB | None | 0 0
  1. //Form code:
  2.  
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Windows.Forms;
  7.  
  8.     public partial class frmCmdInOut : Form
  9.     {
  10.         Process cmdProcess = null;
  11.         StreamWriter stdin = null;
  12.  
  13.         public frmCmdInOut() => InitializeComponent();
  14.  
  15.         private void MainForm_Load(object sender, EventArgs e)
  16.         {
  17.             rtbStdIn.Multiline = false;
  18.             rtbStdIn.SelectionIndent = 20;
  19.         }
  20.  
  21.         private void btnStartProcess_Click(object sender, EventArgs e)
  22.         {
  23.             btnStartProcess.Enabled = false;
  24.             StartCmdProcess();
  25.             btnEndProcess.Enabled = true;
  26.             //stdin.Write((char)13);
  27.         }
  28.  
  29.         private void btnEndProcess_Click(object sender, EventArgs e)
  30.         {
  31.             if (stdin.BaseStream.CanWrite) {
  32.                 stdin.WriteLine("exit");
  33.             }
  34.             btnEndProcess.Enabled = false;
  35.             btnStartProcess.Enabled = true;
  36.             cmdProcess?.Close();
  37.         }
  38.  
  39.         private void rtbStdIn_KeyPress(object sender, KeyPressEventArgs e)
  40.         {
  41.             if (e.KeyChar == (char)Keys.Enter) {
  42.                 if (stdin == null) {
  43.                     rtbStdErr.AppendText("Process not started" + Environment.NewLine);
  44.                     return;
  45.                 }
  46.  
  47.                 e.Handled = true;
  48.                 if (stdin.BaseStream.CanWrite) {
  49.                     stdin.Write(rtbStdIn.Text + Environment.NewLine);
  50.                     stdin.WriteLine();
  51.                     // To write to a Console app, just
  52.                     // stdin.WriteLine(rtbStdIn.Text);
  53.                 }
  54.                 rtbStdIn.Clear();
  55.             }
  56.         }
  57.  
  58.         private void StartCmdProcess()
  59.         {
  60.             var pStartInfo = new ProcessStartInfo {
  61.                 FileName = "cmd.exe",
  62.                 //Batch File Arguments = "/C START /b /WAIT testbatch1.bat",
  63.                 //Test: Arguments = "START /WAIT /K ipconfig /all",
  64.                 Arguments = "START /WAIT",
  65.                 WorkingDirectory = Environment.SystemDirectory,
  66.                 // WorkingDirectory = Application.StartupPath,
  67.                 RedirectStandardOutput = true,
  68.                 RedirectStandardError = true,
  69.                 RedirectStandardInput = true,
  70.                 UseShellExecute = false,
  71.                 CreateNoWindow = true,
  72.                 WindowStyle = ProcessWindowStyle.Hidden,
  73.             };
  74.  
  75.             cmdProcess = new Process {
  76.                 StartInfo = pStartInfo,
  77.                 EnableRaisingEvents = true,
  78.                 // Test without and with this
  79.                 // When SynchronizingObject is set, no need to BeginInvoke()
  80.                 //SynchronizingObject = this
  81.             };
  82.  
  83.             cmdProcess.Start();
  84.             cmdProcess.BeginErrorReadLine();
  85.             cmdProcess.BeginOutputReadLine();
  86.             stdin = cmdProcess.StandardInput;
  87.             stdin.AutoFlush = true;
  88.  
  89.             cmdProcess.OutputDataReceived += (s, evt) => {
  90.                 if (evt.Data != null)
  91.                 {
  92.                     BeginInvoke(new MethodInvoker(() => {
  93.                         rtbStdOut.AppendText(evt.Data + Environment.NewLine);
  94.                         rtbStdOut.ScrollToCaret();
  95.                     }));
  96.                 }
  97.             };
  98.  
  99.             cmdProcess.ErrorDataReceived += (s, evt) => {
  100.                 if (evt.Data != null) {
  101.                     BeginInvoke(new Action(() => {
  102.                         rtbStdErr.AppendText(evt.Data + Environment.NewLine);
  103.                         rtbStdErr.ScrollToCaret();
  104.                     }));
  105.                 }
  106.             };
  107.  
  108.             cmdProcess.Exited += (s, evt) => {
  109.                 stdin?.Dispose();
  110.                 cmdProcess?.Dispose();
  111.             };
  112.         }
  113.     }
  114.  
  115. //Form Designer: Don't overwrite the existing Namespace :)
  116.  
  117.     partial class frmCmdInOut
  118.     {
  119.         private System.ComponentModel.IContainer components = null;
  120.         protected override void Dispose(bool disposing)
  121.         {
  122.             if (disposing && (components != null)) {
  123.                 components.Dispose();
  124.             }
  125.             base.Dispose(disposing);
  126.         }
  127.  
  128.         private void InitializeComponent()
  129.         {
  130.             this.rtbStdOut = new System.Windows.Forms.RichTextBox();
  131.             this.rtbStdErr = new System.Windows.Forms.RichTextBox();
  132.             this.label1 = new System.Windows.Forms.Label();
  133.             this.label2 = new System.Windows.Forms.Label();
  134.             this.label3 = new System.Windows.Forms.Label();
  135.             this.rtbStdIn = new System.Windows.Forms.RichTextBox();
  136.             this.btnStartProcess = new System.Windows.Forms.Button();
  137.             this.btnEndProcess = new System.Windows.Forms.Button();
  138.             this.label4 = new System.Windows.Forms.Label();
  139.             this.SuspendLayout();
  140.             //
  141.             // rtbStdOut
  142.             //
  143.             this.rtbStdOut.BackColor = System.Drawing.Color.MidnightBlue;
  144.             this.rtbStdOut.ForeColor = System.Drawing.Color.White;
  145.             this.rtbStdOut.Location = new System.Drawing.Point(12, 29);
  146.             this.rtbStdOut.Name = "rtbStdOut";
  147.             this.rtbStdOut.Size = new System.Drawing.Size(434, 202);
  148.             this.rtbStdOut.TabIndex = 0;
  149.             this.rtbStdOut.TabStop = false;
  150.             this.rtbStdOut.Text = "";
  151.             //
  152.             // rtbStdErr
  153.             //
  154.             this.rtbStdErr.BackColor = System.Drawing.Color.White;
  155.             this.rtbStdErr.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  156.             this.rtbStdErr.ForeColor = System.Drawing.Color.Black;
  157.             this.rtbStdErr.Location = new System.Drawing.Point(12, 267);
  158.             this.rtbStdErr.Name = "rtbStdErr";
  159.             this.rtbStdErr.Size = new System.Drawing.Size(434, 95);
  160.             this.rtbStdErr.TabIndex = 1;
  161.             this.rtbStdErr.TabStop = false;
  162.             this.rtbStdErr.Text = "";
  163.             //
  164.             // label1
  165.             //
  166.             this.label1.AutoSize = true;
  167.             this.label1.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  168.             this.label1.Location = new System.Drawing.Point(12, 9);
  169.             this.label1.Name = "label1";
  170.             this.label1.Size = new System.Drawing.Size(154, 17);
  171.             this.label1.TabIndex = 2;
  172.             this.label1.Text = "Process Standard Output";
  173.             //
  174.             // label2
  175.             //
  176.             this.label2.AutoSize = true;
  177.             this.label2.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  178.             this.label2.Location = new System.Drawing.Point(12, 247);
  179.             this.label2.Name = "label2";
  180.             this.label2.Size = new System.Drawing.Size(144, 17);
  181.             this.label2.TabIndex = 2;
  182.             this.label2.Text = "Process Standard Error";
  183.             //
  184.             // label3
  185.             //
  186.             this.label3.AutoSize = true;
  187.             this.label3.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  188.             this.label3.Location = new System.Drawing.Point(12, 377);
  189.             this.label3.Name = "label3";
  190.             this.label3.Size = new System.Drawing.Size(247, 17);
  191.             this.label3.TabIndex = 4;
  192.             this.label3.Text = "Enter command (Process Standard Input)";
  193.             //
  194.             // rtbStdIn
  195.             //
  196.             this.rtbStdIn.BackColor = System.Drawing.Color.White;
  197.             this.rtbStdIn.DetectUrls = false;
  198.             this.rtbStdIn.Font = new System.Drawing.Font("Segoe UI", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  199.             this.rtbStdIn.ForeColor = System.Drawing.Color.Black;
  200.             this.rtbStdIn.Location = new System.Drawing.Point(12, 397);
  201.             this.rtbStdIn.Name = "rtbStdIn";
  202.             this.rtbStdIn.Size = new System.Drawing.Size(434, 28);
  203.             this.rtbStdIn.TabIndex = 0;
  204.             this.rtbStdIn.Text = "";
  205.             this.rtbStdIn.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.rtbStdIn_KeyPress);
  206.             //
  207.             // btnStartProcess
  208.             //
  209.             this.btnStartProcess.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
  210.             this.btnStartProcess.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  211.             this.btnStartProcess.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  212.             this.btnStartProcess.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  213.             this.btnStartProcess.Location = new System.Drawing.Point(357, 455);
  214.             this.btnStartProcess.Name = "btnStartProcess";
  215.             this.btnStartProcess.Size = new System.Drawing.Size(89, 29);
  216.             this.btnStartProcess.TabIndex = 2;
  217.             this.btnStartProcess.Text = "Start Process";
  218.             this.btnStartProcess.UseVisualStyleBackColor = false;
  219.             this.btnStartProcess.Click += new System.EventHandler(this.btnStartProcess_Click);
  220.             //
  221.             // btnEndProcess
  222.             //
  223.             this.btnEndProcess.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
  224.             this.btnEndProcess.Enabled = false;
  225.             this.btnEndProcess.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  226.             this.btnEndProcess.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  227.             this.btnEndProcess.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  228.             this.btnEndProcess.Location = new System.Drawing.Point(253, 455);
  229.             this.btnEndProcess.Name = "btnEndProcess";
  230.             this.btnEndProcess.Size = new System.Drawing.Size(89, 29);
  231.             this.btnEndProcess.TabIndex = 1;
  232.             this.btnEndProcess.Text = "End Process";
  233.             this.btnEndProcess.UseVisualStyleBackColor = false;
  234.             this.btnEndProcess.Click += new System.EventHandler(this.btnEndProcess_Click);
  235.             //
  236.             // label4
  237.             //
  238.             this.label4.BackColor = System.Drawing.Color.White;
  239.             this.label4.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  240.             this.label4.ForeColor = System.Drawing.Color.Black;
  241.             this.label4.Location = new System.Drawing.Point(14, 400);
  242.             this.label4.Name = "label4";
  243.             this.label4.Size = new System.Drawing.Size(17, 20);
  244.             this.label4.TabIndex = 6;
  245.             this.label4.Text = ">";
  246.             //
  247.             // MainForm
  248.             //
  249.             this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  250.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
  251.             this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(32)))));
  252.             this.ClientSize = new System.Drawing.Size(458, 494);
  253.             this.Controls.Add(this.label4);
  254.             this.Controls.Add(this.btnEndProcess);
  255.             this.Controls.Add(this.btnStartProcess);
  256.             this.Controls.Add(this.label3);
  257.             this.Controls.Add(this.rtbStdIn);
  258.             this.Controls.Add(this.label2);
  259.             this.Controls.Add(this.label1);
  260.             this.Controls.Add(this.rtbStdErr);
  261.             this.Controls.Add(this.rtbStdOut);
  262.             this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  263.             this.ForeColor = System.Drawing.Color.White;
  264.             this.Name = "MainForm";
  265.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  266.             this.Text = "System.Diagnostics.Process  Tests";
  267.             this.Load += new System.EventHandler(this.MainForm_Load);
  268.             this.ResumeLayout(false);
  269.             this.PerformLayout();
  270.  
  271.         }
  272.  
  273.         private System.Windows.Forms.RichTextBox rtbStdOut;
  274.         private System.Windows.Forms.RichTextBox rtbStdErr;
  275.         private System.Windows.Forms.Label label1;
  276.         private System.Windows.Forms.Label label2;
  277.         private System.Windows.Forms.Label label3;
  278.         private System.Windows.Forms.RichTextBox rtbStdIn;
  279.         private System.Windows.Forms.Button btnStartProcess;
  280.         private System.Windows.Forms.Button btnEndProcess;
  281.         private System.Windows.Forms.Label label4;
  282.     }
  283.  
  284.  
  285.  
  286.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement