0

So I got an assignment for school this asks of me to make a simple ping tool. Just to be clear this is my first time getting anything done in c#. Correct me on anything I did wrong here I am here to learn so I will post my code down here and then maybe you guys could add and complete or tell me whats wrong already thank you for reading and taking the time to try and help me.

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
     {
        public object StartInfo { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            String sct = "/C ping " + textBox1.Text;



            System.Diagnostics.Process.Start("cmd.exe", sct);





}

        public void label1_Click(object sender, EventArgs e)
        {

        }

        public void process1_Exited(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs 
        {

        }
    }
sme
  • 4,023
  • 3
  • 28
  • 42
  • There is no point in calling a process that calls cmd that calls ping. You could just create a process that calls ping. That being said, you should check this question, seems to answer your problem https://stackoverflow.com/questions/18588659/redirect-process-output-c-sharp – Cleptus Oct 31 '18 at 22:37
  • Help with what? You didnt ask a question or describe a problem. The code is mostly empty event handlers which of course mean nothing. – Ňɏssa Pøngjǣrdenlarp Oct 31 '18 at 22:38
  • 1
    Possible duplicate of [Redirect process output C#](https://stackoverflow.com/questions/18588659/redirect-process-output-c-sharp) – Markus Safar Oct 31 '18 at 22:47

1 Answers1

1

enter image description here

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 WindowsFormsApp26
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Process cmd = new Process();
        private void Form1_Load(object sender, EventArgs e)
        {

            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.OutputDataReceived += Cmd_OutputDataReceived;
            cmd.Start();
            cmd.BeginOutputReadLine();

        }

        private void Cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            SetText(e.Data);

        }

        private void button1_Click(object sender, EventArgs e)
        {

            cmd.StandardInput.WriteLine($@"{textBox1.Text}");
            cmd.StandardInput.Flush();

        }

        delegate void StringArgReturningVoidDelegate(string text);
        private void SetText(string text)
        { // InvokeRequired required compares the thread ID of the
          // calling thread to the thread ID of the creating thread.
          // If these threads are different, it returns true.

            if (text != null)
            {


                if (listBox1.InvokeRequired)
                {
                    StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetText);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    listBox1.Items.Add(text);

                }

            }
        }
    }
}
Mr.feel
  • 315
  • 1
  • 3
  • 12