I have chosen a console type project in Visual 2010 and want to know how to write to the cmd line.

For example, I want to be able to automatically send a string (like ipconfig) and then have the results displayed in the box.

eg;

Console.WriteLine("ipconfig");        // Send cmd

Thread.Sleep(1000);                   // Sleep for 1s

for (int i = 0; i < 5; i++)           // Display the first 5 lines
        {
          Console.ReadLine();
         }

Hi,
Here is the code I wrote, It works, Let me know if you have any questions.

public void cmd(object command)
        {
            try
            {
                string output;
               //First of all define the main process

                System.Diagnostics.ProcessStartInfo Pstart = new System.Diagnostics.ProcessStartInfo("cmd", "/c" + command);

                Pstart.RedirectStandardOutput = true;
                Pstart.UseShellExecute = false;

                System.Diagnostics.Process P = new System.Diagnostics.Process();

                P.StartInfo = Pstart;

                P.Start();
                Thread.Sleep(10000);

                output = P.StandardOutput.ReadToEnd();

                Console.WriteLine("{0}",output);
                 

            }

            catch (Exception e)
            {
                Console.WriteLine("{0}", e.StackTrace);
            
            }
        
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.