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 
        {

        }
    }
3
  • 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 stackoverflow.com/questions/18588659/… Commented Oct 31, 2018 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. Commented Oct 31, 2018 at 22:38
  • 1
    Possible duplicate of Redirect process output C# Commented Oct 31, 2018 at 22:47

1 Answer 1

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);

                }

            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.